Announcement

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

  • two notes when using twoway graph, by()

    Hi all,

    I'm making a graph (see below) that uses the twoway graph by() option. As you can see, because i'm specifying note() in the main options of the twoway and because the by() option just repeats them, i'm ending up with one note which is repeated for each graph. What i'd like to do is adjust the notes so they each reflect the N of the sample i'm plotting.

    Is this possible to do with syntax? if not, i'll just do it in the graph editor as this graph is otherwise exactly what i want. See fictitious data example + code below to replicate the graph

    Best,
    Chris

    Code:
    clear
    input float(wk_dist_eff_nov16 election_2016_11 motorvoter)
    26 1 1
     8 1 0
     8 0 0
    32 1 0
    29 1 0
    32 0 1
    37 1 0
     3 0 1
     7 1 0
     4 1 0
    33 1 0
    30 1 0
    29 1 0
     5 1 0
     4 1 0
    31 1 0
     3 1 0
    26 1 0
    25 1 0
    16 1 0
    33 1 0
    23 1 1
    35 1 0
    28 1 1
    32 1 0
    33 1 0
    28 1 0
    26 1 0
    33 1 0
    30 1 0
     5 1 0
     4 1 0
     4 1 0
    12 1 0
    25 0 0
     3 1 0
    32 1 0
    32 1 0
     3 1 0
    32 1 0
    34 1 0
    31 1 0
     8 1 0
     5 1 0
     4 1 0
     8 1 0
    27 1 0
    12 1 0
    19 1 1
     8 0 0
    end
    label values election_2016_11 election_2016_11_l
    label def election_2016_11_l 0 "No vote", modify
    label def election_2016_11_l 1 "Vote", modify
    label values motorvoter motorvoter
    label def motorvoter 0 "Regular", modify
    label def motorvoter 1 "Motor Voter", modify
    
    
    twoway (lowess election_2016_11 wk_dist_eff_nov16, lcol(black) lwidth(medium)), by(motorvoter, title("Propensity to vote against distance in weeks between registration and election", col(black) size(medsmall)) note("")) ///
           bgcol(white) graphregion(col(white)) ylab(,angle(0) format(%9.2f)) ysc(extend) ///
           xtitle("Weeks from 2016 general election") ///
           xsc(titlegap(*5)) ytitle("Lowess smoothed values of likelihood to vote") ysc(titlegap(*5)) xlab(#10) note("{it:n}= 89,510") name(week_lowess, replace)
    Click image for larger version

Name:	weekly_lowess_nov16.png
Views:	1
Size:	66.8 KB
ID:	1595655

    Last edited by Chris Larkin; 01 Mar 2021, 06:25.

  • #2
    I don't know a way to get notes that are different for each graph using by(). Other than producing separate graphs and using graph combine, here are a couple of perhaps more charming work-arounds.


    Code:
    sysuse auto, clear
    set scheme s1color 
    
    * method 1: use an extra variable with text that appears in the plot region 
    * here, two passes were used to identify a good position for text 
    bysort foreign : gen toshow = "{it:n} = " + string(_N)
    scatter mpg weight , by(foreign)
    gen y = 11
    gen x = 1800 
    
    scatter mpg weight , ms(Oh) by(foreign, legend(off) note("")) ///
    || scatter y x, ms(none) mla(toshow) mlabsize(medium)         ///
    ytitle("`: var label mpg'") xtitle("`: var label weight'") name(G1, replace)
    
    * method 2: working at values or value labels of by() variable 
    decode foreign, gen(origin)
    bysort origin : replace origin = origin + " ({it:n} = " + string(_N) + ")"
    
    scatter mpg weight , ms(Oh) by(origin, legend(off) note("")) name(G2, replace)
    Click image for larger version

Name:	larkin_G1.png
Views:	1
Size:	42.1 KB
ID:	1595668


    Click image for larger version

Name:	larkin_G2.png
Views:	1
Size:	42.6 KB
ID:	1595669

    Comment


    • #3
      Thanks Nick -- two quite nice options. I'll play around with it and see which I prefer. I may still just end up using the graph editor.

      Imagine this is pretty low on the priority list for Stata Corp but a similar syntax to t1title, t2title, etc. for notes, captions, etc. would be great

      Comment


      • #4
        You can't use those others to get different titles either, so far as I can see. What might be helpful here is some kind of vartitle() option that lets you look inside a variable for a title, and if it is, erm, variable, it would display different text in different panels.

        Comment


        • #5
          https://www.stata-journal.com/articl...article=gr0051 was about traffic in the opposite direction, putting notes in the body of the plot.

          Comment


          • #6
            Interesting. Perhaps something analogous to specifying graph parameters when there are multiple y/x-axes could work. So instead of ylabel(..., axis(1)) where the user has specified tw (lpoly yvar xvar, yaxis(1)), they could write something like note("some interesting information", graph(1)) where the user has specified by()

            Comment

            Working...
            X