Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Formatting graph legend and markers

    Hi. I am trying to format the legend size of the graph, but when I add "legend(size(small))" to the code below, I get the strange legend in the graph appear. However, if I take off the legend bit, it's shows as the dots as "Point estimate" and the shaded region as "Confidence interval", which is what I want.

    I'm also unsure why the two points at either end are different in size and color.

    Any help in sorting out the legend and the end points will be much appreciated.

    Code:
    eventdd rate_con_avg7day, hdfe absorb(state) vce(cluster state) timevar(time_to_event) ///
     graph_op(mcolor(gs1) msize(vsmall)  ylabel(, labsize(2)) legend(size(small)) ///
     xtitle("Time (weeks)", size(small)) xlabel(-6(5)35,  labsize(2))) ci(rarea , color(gs14%33))  leads(6) lags(35) accum
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	119.1 KB
ID:	1770468

  • #2
    eventdd is a community-contributed command, as you're asked to explain (FAQ Advice #12). The most recent version appears to be that on SSC.

    There isn't a data example here that can be used to experiment (same place). In this particular case, I doubt that would help much.

    Evidently your variation on graph defaults isn't being handled cleanly by the code.

    A simpler alternative is to try making your changes in the Graph Editor.

    If that doesn't work and you don't get a better answer I would contact the program authors directly.

    All that said, I am not clear why a legend is really needed here at all.

    Comment


    • #3
      Another alternative is to

      Code:
      set trace on 
      
      set tracedepth 1
      and try to follow what is happening with your extra graph options. The handling of graphics inside the program is a little complicated. That's not a criticism, but it means that I was unable to guess quickly how your syntax would work out.

      Comment


      • #4
        As an alternative to set trace on that give a bit less output you can use graph describe to see the twoway command that was used to create the graph.
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Maarten Buis makes an excellent point.

          It would be good to have expository papers on debugging. I have a vague recollection of starting to write one once, but either I got bored after a few hundred words or the topic seemed too big and formless.

          Comment


          • #6
            Thanks Nick Cox and Maarten Buis. The legend I can do without and that's okay. However, I'm unable to figure how to edit the two end points in graph editor. I'd greatly appreciate any help here. I've also included the output from graph describe below, which I don't know how to make sense of.

            Code:
            graph describe
            ---------------------------------------------------------------------------------------------------------------------------- begin graph ---
            - if d(`=c(born_date)') < d(23Jul2004) {
            = if d(13 Jul 2023) < d(23Jul2004) {
              di as err "your Stata executable is out of date"
              di as err "    type -update executable- at the Stata prompt"
              exit 498
              }
            - local ver = string(_caller())
            - if (_caller() < 8.2) version 8
            - else if (_caller() < 10 ) version 8.2
            - else version 10
            - gdi record = yes
            - gdi maybedraw = yes
            - if "`._Gr_Global.isa'" == "" {
            = if "class" == "" {
              ._Gr_Global = .global_g.new
              }
            - ._Gr_Global.callerver = "`ver'"
            = ._Gr_Global.callerver = "18"
            - capture noisily Graph `0'
            = capture noisily Graph describe
            
            Graph stored in memory
            
                    name:  Graph
                  format:  live
                 created:  11 Jan 2025 13:36:52
                  scheme:  s1color
                    size:  4 x 5.5
                dta file:  C:\Users\dsouz\Dropbox\UNC\Dissertation\Dissertation\Aim 1\Data\analysis\input\matching.dta dated 2 Sep 2024 18:12
                 command:  twoway rarea __000003 __000002 __000000, color(gs14%33) yline(0, lcolor(red)) || scatter __000001 __000000, mcolor(gs1)
                           msize(vsmall) ylabel(, labsize(2)) legend(size(small)) xtitle("Time (weeks)", size(small)) xlabel(-6(5)35, labsize(2))
                           xline(-1, lcolor(black) lpattern(solid)) || scatter __000001 __000000 if __000000==-6 | __000000==35,
            - local rc = _rc
            - gdi record = yes
            - gdi maybedraw = yes
            - gdi end
            - exit `rc'
            = exit 0

            Comment


            • #7
              Scott Rick / dsouz

              Thanks for the detail. I am as puzzled as before.

              Comment


              • #8
                Scott Rick hi, use the example dataset of -eventdd- command, I can replicate your problem. I guess reasons of it are: 1) you use tempfile to graph as the example guides you to do, so (when something get wrong, however, I don't know what's going wrong) there are tempvar in your legend; 2) as far as I know, you should specify explicitly the legend keys by using order() option, for example, legend(size(small) order(1 "Confidence Interval" 2 "Point Estimates")); 3) the two points at either end are different in size and color because you set leads and lags option, if you take them off, the problem will disappear; or 4) if you want to set leads and lags, you should add endpoints_op to modify the plot, for example, endpoints_op(mcolor(gs1) msize(vsmall)). I will post my illustrative code and graph below.

                Code:
                webuse set www.damianclarke.net/stata/
                webuse bacon_example.dta, clear
                gen timeToTreat = year - _nfd
                tempfile example
                save "`example'", replace
                eventdd asmrs pcinc asmrh cases, hdfe absorb(year) timevar(timeToTreat) ///
                 graph_op(mcolor(gs1) msize(vsmall)  ylabel(, labsize(2)) legend(size(small)) ///
                 xtitle("Time (weeks)", size(small)) xlabel(-6(5)35,  labsize(2))) ///
                 ci(rarea , color(gs14%33))  leads(21) lags(27) accum
                Code:
                     command:  twoway rarea __000003 __000002 __000000, color(gs14%33) yline(0, lcolor(red)) || scatter __000001
                
                               __000000, mcolor(gs1) msize(vsmall) ylabel(, labsize(2)) legend(size(small)) xtitle("Time
                               (weeks)", size(small)) xlabel(-6(5)35, labsize(2)) xline(-1, lcolor(black) lpattern(solid)) ||
                               scatter __000001 __000000 if __000000==-21 | __000000==27,
                Code:
                eventdd asmrs pcinc asmrh cases, hdfe absorb(year) timevar(timeToTreat) ///
                 graph_op(mcolor(gs1) msize(vsmall)  ylabel(, labsize(2)) ///
                 legend(size(small) order(1 "Confidence Interval" 2 "Point Estimates")) ///
                 xtitle("Time (weeks)", size(small)) xlabel(-6(5)35,  labsize(2))) ///
                 ci(rarea , color(gs14%33)) leads(21) lags(27) accum endpoints_op(mcolor(gs1) msize(vsmall))
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	187.1 KB
ID:	1770664



                Click image for larger version

Name:	Graph2.png
Views:	1
Size:	192.7 KB
ID:	1770663

                Comment


                • #9
                  Chen Samulsion Thanks a lot! That solved the problem and the explanation was very helpful!

                  Comment

                  Working...
                  X