Announcement

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

  • Manually inputting extra key on graphs

    Hello,

    I'm making a combined graph. There are 6 graphs in total, with two rows of 3. The top 3 show distributions of survey scores belonging to a control group, and the bottom 3 show the distributions for the treatment group.

    The problem i'm running into is that if I use graph combine I end up with the legend for the control group half way between the two rows and the legend for the treatment group at the bottom of the plot region (where I want both keys and text to be). If I use grc1leg (SSC) it will only include one of the keys and associated text because the legend for the single graph (that grc1leg draws from) is only for a single variable.

    I've tried forcing the histogram to show two keys, by specifying lab(1 "label one") and lab(2 "label two"). This gives me the text that I want for the second key, but naturally only one key shows up when I render the graphs. What I want is for there to be a legend at the bottom of the combined graph, with two keys (one gs7 and another gs13) indicating that the top row is for the control group and the bottom row is for the treatment group.

    I share data and code below to replicate the problem.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(treatment q1 q2 q3 newobs)
    1 14 14 12 0
    1 13  8  7 0
    1  9 11  6 0
    1 11  6 11 0
    1  8 10 11 0
    1  8  6  8 0
    0  .  .  . 0
    0  2  5  4 0
    0  9 14 14 0
    0  2  4  4 0
    0  5  5  4 0
    0  .  .  . 0
    1  4  8  5 0
    1 13 14  . 0
    1  5 12  5 0
    1  2  4  7 0
    1  4  4  2 0
    1 11  9 10 0
    1 10  8  7 0
    1  4  6  4 0
    1 14 14 13 0
    1 13 10 10 0
    1  6 11 14 0
    0  6  9 10 0
    0  4  5  2 0
    0  8 10 11 0
    0  8 11  4 0
    0  9 10 10 0
    0  5  7  4 0
    0 14 14 11 0
    0 11 11 10 0
    0 10 11 11 0
    0 10 13 10 0
    1 14 10 14 1
    1 14 13 10 1
    1 14 10 12 1
    1 12  6  6 1
    1 12  8  6 1
    1 10  8  8 1
    1  8  8  8 1
    1  9  8  8 1
    1  9  8  7 1
    1 10  8 11 1
    1  8  8  9 1
    1 10  8  8 1
    1 10  8  9 1
    1 11  8  8 1
    1 10 10  8 1
    1 11  7  8 1
    1 10  7  8 1
    1 11  7  6 1
    0  .  .  . 1
    0  .  .  . 1
    0  .  .  . 1
    0  2  5  4 1
    0  2  4  5 1
    0  2  4  4 1
    0  8 10 13 1
    0 10 14 14 1
    0 11 13 11 1
    0  2  5  4 1
    0  2  5  5 1
    0  2  4  5 1
    0  4  5  5 1
    0  5  4  4 1
    0  5  5  4 1
    0  .  .  . 1
    0  .  .  . 1
    0  .  .  . 1
    1  5  6  4 1
    1  4  7  5 1
    1  4  7  4 1
    1 14 10  . 1
    1 12 10  . 1
    1 11 10  . 1
    1  5 11  5 1
    1  4 13  4 1
    1  4 14  4 1
    1  2  4  8 1
    1  2  5  8 1
    1  2  5  6 1
    1  5  4  2 1
    1  4  5  2 1
    1  4  4  2 1
    1 11 10  8 1
    1 10 10 10 1
    1  9  9  9 1
    1  9  9  7 1
    1  9 11  7 1
    1 11  9  7 1
    1  5  7  5 1
    1  4  8  4 1
    1  5  7  5 1
    1 14 11 13 1
    1 13 12 13 1
    1 11 12 14 1
    1 14 10 14 1
    1 14 10 14 1
    1 12 12 12 1
    1  6 14 14 1
    end

    Code:
    quietly{
        foreach var of varlist q1 q2 q3{
            summarize `var' if treat==0
            local Cmin2SD = round(`=`r(mean)' - (`r(sd)'*2)', 0.1)
            local Cmin1SD = round(`=`r(mean)' - `r(sd)'', 0.1)
            local Cmean = round(`r(mean)', 0.1)
            local Cplus1SD = round(`=`r(mean)' + `r(sd)'', 0.1)
            local Cplus2SD = round(`=`r(mean)' + (`r(sd)'*2)', 0.1)
            
            summarize `var' if treat==1
            local Tmin2SD = round(`=`r(mean)' - (`r(sd)'*2)', 0.1)
            local Tmin1SD = round(`=`r(mean)' - `r(sd)'', 0.1)
            local Tmean = round(`r(mean)', 0.1)
            local Tplus1SD = round(`=`r(mean)' + `r(sd)'', 0.1)
            local Tplus2SD = round(`=`r(mean)' + (`r(sd)'*2)', 0.1)
    
            if "`var'"=="q1"            local ytit "Frequency of response"
            if "`var'"!="q1"            local ytit ""
            
            
            if "`var'"=="q1"            local xtit "Scale one"
            if "`var'"=="q2"            local xtit "Scale two" 
            if "`var'"=="q3"            local xtit "Scale three"    
            
        hist `var' if treat==0, discrete col(gs7) graphregion(col(white)) bgcol(white) freq xaxis(1 2) yaxis(1 2) ylabel(0(2)16, grid format(%9.0f) angle(0)) ///
        xlabel(`Cmean' "mean" `Cmin2SD' "-2 s.d." `Cmin1SD' "-1 s.d." `Cplus1SD' "+1 s.d." `Cplus2SD' "+2 s.d.", axis(2) grid gmax angle(-0.0001) labgap(3)) ///
        xtitle("", axis(2)) xtitle("`xtit'", axis(1)) xlabel(0(1)14) ytitle("", axis(2)) ytitle("`ytit'", axis(1)) lcol(white) ///
        ysc(r(0/16)) legend(lab(1 "control") lab(2 "treatment") region(c(none)) order( 1 2 ) size(3) cols(2)) ylab(, nolabel noticks axis(2)) name(`var'_0, replace) nodraw
    
        hist `var' if treat==1, discrete col(gs13) graphregion(col(white)) bgcol(white) freq xaxis(1 2) yaxis(1 2) ylabel(0(1)16, grid format(%9.0f) angle(0)) ///
        xlabel(`Tmean' "mean" `Tmin2SD' "-2 s.d." `Tmin1SD' "-1 s.d." `Tplus1SD' "+1 s.d." `Tplus2SD' "+2 s.d.", axis(2) grid gmax angle(-0.0001) labgap(3)) ///
        xtitle("", axis(2)) xtitle("", axis(1)) xlabel(0(1)14) ytitle("", axis(2)) ytitle("`ytit'", axis(1)) lcol(white) ///
        ysc(r(0/16)) legend(lab(1 "control") lab(2 "treatment") region(c(none)) order( 1 2 ) size(3) cols(2)) ylab(, nolabel noticks axis(2)) name(`var'_1, replace) nodraw
    
        }
    }
    grc1leg q1_0 q2_0 q3_0, graphregion(color(white)) leg(q1_0) ///
        imargin(vsmall) title("", color(black) size(med)) l1("", col(black) size(small)) cols(3) xsize(12) ysize(4) ///
        name(combContr, replace)                                                 
        graph display combContr, xsize(12) ysize(4)         
        
    grc1leg q1_1 q2_1 q3_1, graphregion(color(white)) leg(q1_1) ///
        imargin(vsmall) title("", color(black) size(med)) l1("", col(black) size(small)) cols(3) xsize(12) ysize(4) ///
        name(combTreat, replace)                                                         
        graph display combTreat, xsize(12) ysize(4)
       
    grc1leg combContr combTreat, graphregion(color(white)) imargin(vsmall) ///
    title("", color(black) size(small)) l1("", col(black) size(small)) cols(1) xsize(12) ysize(8)
    
    graph combine combContr combTreat, graphregion(color(white)) imargin(vsmall) ///
    title("", color(black) size(small)) l1("", col(black) size(small)) cols(1) xsize(12) ysize(8)
    A second problem is that I only want there to be xtitles for one of the rows. I've placed these on the bottom of the top row so it looks like they're in the middle of the plot region, and are associated vertically with each column. However, when doing this the xtitles are very close to the top row's x-axis. I can specify xscale(titlegap(*5)) and this does increase the space between the x-axis and the xtitle, but then it squashes the top row to accommodate for the space i've created. This makes comparing the top and bottom rows visually harder. Any ideas on this would also be gratefully appreciated.

  • #2
    One of my graphical mottos is Lose the legend if you can! Kill the key! So, I am going to suggest ways of using at most one key. In fact, most of my suggestions use none at all.

    The key (all puns should be considered intentional) is to restructure your data, so that you have sufficiently few variables that everything comes out of one command, and all this fudging and nudging is reduced, if not avoided altogether.

    I used tabplot (Stata Journal), stripplot (SSC), qplot (Stata Journal). These are just some suggestions and others are easy too. Whether treatments should be close, or scales, is a matter of your goals and of what works best to show structure in the data.

    newobs appears irrelevant.

    The graphs are in the wrong order below: 1 3 4 2. Sorry about that.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(treatment q1 q2 q3 newobs)
    1 14 14 12 0
    1 13  8  7 0
    1  9 11  6 0
    1 11  6 11 0
    1  8 10 11 0
    1  8  6  8 0
    0  .  .  . 0
    0  2  5  4 0
    0  9 14 14 0
    0  2  4  4 0
    0  5  5  4 0
    0  .  .  . 0
    1  4  8  5 0
    1 13 14  . 0
    1  5 12  5 0
    1  2  4  7 0
    1  4  4  2 0
    1 11  9 10 0
    1 10  8  7 0
    1  4  6  4 0
    1 14 14 13 0
    1 13 10 10 0
    1  6 11 14 0
    0  6  9 10 0
    0  4  5  2 0
    0  8 10 11 0
    0  8 11  4 0
    0  9 10 10 0
    0  5  7  4 0
    0 14 14 11 0
    0 11 11 10 0
    0 10 11 11 0
    0 10 13 10 0
    1 14 10 14 1
    1 14 13 10 1
    1 14 10 12 1
    1 12  6  6 1
    1 12  8  6 1
    1 10  8  8 1
    1  8  8  8 1
    1  9  8  8 1
    1  9  8  7 1
    1 10  8 11 1
    1  8  8  9 1
    1 10  8  8 1
    1 10  8  9 1
    1 11  8  8 1
    1 10 10  8 1
    1 11  7  8 1
    1 10  7  8 1
    1 11  7  6 1
    0  .  .  . 1
    0  .  .  . 1
    0  .  .  . 1
    0  2  5  4 1
    0  2  4  5 1
    0  2  4  4 1
    0  8 10 13 1
    0 10 14 14 1
    0 11 13 11 1
    0  2  5  4 1
    0  2  5  5 1
    0  2  4  5 1
    0  4  5  5 1
    0  5  4  4 1
    0  5  5  4 1
    0  .  .  . 1
    0  .  .  . 1
    0  .  .  . 1
    1  5  6  4 1
    1  4  7  5 1
    1  4  7  4 1
    1 14 10  . 1
    1 12 10  . 1
    1 11 10  . 1
    1  5 11  5 1
    1  4 13  4 1
    1  4 14  4 1
    1  2  4  8 1
    1  2  5  8 1
    1  2  5  6 1
    1  5  4  2 1
    1  4  5  2 1
    1  4  4  2 1
    1 11 10  8 1
    1 10 10 10 1
    1  9  9  9 1
    1  9  9  7 1
    1  9 11  7 1
    1 11  9  7 1
    1  5  7  5 1
    1  4  8  4 1
    1  5  7  5 1
    1 14 11 13 1
    1 13 12 13 1
    1 11 12 14 1
    1 14 10 14 1
    1 14 10 14 1
    1 12 12 12 1
    1  6 14 14 1
    end
    
    gen id = _n
    
    reshape long q, i(id) j(scale)
    
    label var q whatever
    
    tabplot scale q, by(treatment) xasis barw(1) xla(1/14) blcolor(blue) bfcolor(blue*0.4) showval name(G1, replace)
    
    tabplot treatment q, by(scale, compact row(1)) xasis barw(1) xla(1(2)15) blcolor(blue) bfcolor(blue*0.4) showval name(G2, replace)
    
    * the horizontal lines denote means
    stripplot q, over(treatment) by(scale, row(1) compact) vertical stack refline(lc(orange) lw(thick)) ///
    reflinestretch(0.2)  ms(sh)  mc(blue) aspect(1) name(G3, replace)
    
    qplot q, over(treatment) by(scale, row(1) compact) yla(, ang(h)) aspect(1) xla(0(.25)1) name(G4, replace)
    Click image for larger version

Name:	larkin1.png
Views:	1
Size:	19.7 KB
ID:	1449529

    Click image for larger version

Name:	larkin3.png
Views:	1
Size:	24.7 KB
ID:	1449531

    Click image for larger version

Name:	larkin4.png
Views:	1
Size:	27.5 KB
ID:	1449532

    Click image for larger version

Name:	larkin2.png
Views:	1
Size:	24.8 KB
ID:	1449530

    Last edited by Nick Cox; 18 Jun 2018, 11:50.

    Comment


    • #3
      Hi Nick,

      Thanks for these ideas. I love your motto, in principle (as above re: puns being intentional). Reshaping is a neat solution and I can see it opens up a bunch of other graphing options. That said, I am very close to what I want (see below), and I like being able to show the distribution by standard deviation on the second x-axis, which I’m not sure is possible with qplot (the one I might be tempted to go with from the above). You've given be an idea though; if i cannot manually input a second key, then I will just change the corresponding y-axes to 'control' and 'treated' respectively.
      Click image for larger version

Name:	statalist.png
Views:	1
Size:	181.8 KB
ID:	1449654

      Comment

      Working...
      X