Announcement

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

  • Align boxplot and line graph

    Dear Statalisters,

    I'm trying to combine a line graph with a boxplot in one row. The axes have the same scale, so I would like them to align nicely. However, the ycommon option doesn't work for boxplots and twoway graphs. Is there another way to align them?

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	62.6 KB
ID:	1510830


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long incgroup_th int year double ka
    1 1995  .6523464458247067
    1 1996  .6502027260179435
    1 1997  .5948507180650038
    1 1998  .5661706349206348
    1 1999   .582459372637944
    1 2000  .5714285714285715
    1 2001  .5798826777087647
    1 2002  .6030803571428571
    1 2003  .5980456349206349
    1 2004  .6040013227513229
    1 2005  .5784288194444445
    1 2006  .5796006944444444
    1 2007  .5569878472222222
    1 2008  .5958333333333333
    1 2009  .6405864197530865
    1 2010  .6405864197530864
    1 2011  .6378086419753086
    1 2012             .69375
    1 2013  .6939814814814814
    1 2014  .6677777777777777
    1 2015  .6677777777777777
    1 2016  .6227777777777777
    2 1995  .5054783950617284
    2 1996 .46535126396237514
    2 1997  .4997170781893004
    2 1998  .5056327160493828
    2 1999  .4841880341880342
    2 2000  .5118357487922706
    2 2001  .5165509259259259
    2 2002  .5051587301587303
    2 2003 .48673469387755103
    2 2004  .4671075837742504
    2 2005  .5032480893592006
    2 2006  .5015079365079366
    2 2007  .5085231193926846
    2 2008  .4891025641025642
    2 2009  .5119658119658119
    2 2010  .5190821256038647
    2 2011  .5450231481481481
    2 2012  .5047777777777778
    2 2013  .5327546296296296
    2 2014  .5484444444444443
    2 2015              .5625
    2 2016  .5543438208616779
    3 1995  .4632330246913581
    3 1996 .43595679012345673
    3 1997  .4049382716049383
    3 1998 .41666666666666674
    3 1999  .4141447368421053
    3 2000 .39093750000000005
    3 2001  .3097587719298246
    3 2002   .306140350877193
    3 2003 .26715686274509803
    3 2004 .33596491228070174
    3 2005 .34047619047619054
    3 2006 .34920634920634924
    3 2007  .3791062801932367
    3 2008 .45425000000000004
    3 2009 .46924999999999994
    3 2010 .46495535714285735
    3 2011  .4469907407407407
    3 2012  .5079861111111111
    3 2013  .4829861111111111
    3 2014 .49134615384615377
    3 2015 .46249999999999997
    3 2016            .496875
    4 1995 .13322482638888888
    4 1996 .14518229166666669
    4 1997  .1890522875816993
    4 1998 .16838235294117646
    4 1999  .1654411764705882
    4 2000 .15073529411764702
    4 2001 .14264705882352938
    4 2002 .13749999999999998
    4 2003               .145
    4 2004  .1173611111111111
    4 2005  .1527777777777778
    4 2006 .15460526315789477
    4 2007 .16052631578947368
    4 2008 .15999999999999998
    4 2009 .17006944444444444
    4 2010  .1663194444444444
    4 2011 .16506944444444444
    4 2012 .17564599483204135
    4 2013 .21306818181818182
    4 2014 .21235795454545456
    4 2015 .21511627906976752
    4 2016 .20581395348837203
    end
    label values incgroup_th incomegroup5
    label def incomegroup5 1 "Low Income", modify
    label def incomegroup5 2 "Lower Middle Income", modify
    label def incomegroup5 3 "Upper Middle Income", modify
    label def incomegroup5 4 "High Income", modify
    
    separate ka, by(incgroup_th) veryshortlabel
    gen lastyear = ka if year == 2016
    line ka? year, ylabel(0(0.2)1, format(%2.1g)) legend(off) xscale(range(2022)) || ///
    scatter lastyear year, mlabel(incgroup_th) mcolor(none) name(line, replace)
    
    graph box ka, over(incgroup_th, label(labsize(small))) name(box, replace) ylabel(0(0.2)1, format(%2.1g))
    
    graph combine line box, iscale(0.5) ycommon

  • #2
    You have a title and ticks on the x-axis in the line graph, which causes the misalignment. I would do 2 things

    1, Introduce a line break in the description of categories in the box plot, e.g.,

    Code:
    label def incomegroup5 1 `" "Low"   "Income" "', modify
    2. Add ticks in the categorical axis of the box plot.

    Without fully implementing 1, I get

    Code:
    label values incgroup_th incomegroup5
    label def incomegroup5 1 `" "Low"   "Income" "', modify
    label def incomegroup5 2 "Lower Middle Income", modify
    label def incomegroup5 3 "Upper Middle Income", modify
    label def incomegroup5 4 "High Income", modify
    
    separate ka, by(incgroup_th) veryshortlabel
    gen lastyear = ka if year == 2016
    line ka? year, ylabel(0(0.2)1, format(%2.1g)) legend(off) xscale(range(2022)) || ///
    scatter lastyear year, mlabel(incgroup_th) mcolor(none) name(line, replace)
    
    graph box ka, over(incgroup_th, label(ticks))  name(box, replace) ylabel(0(0.2)1, format(%2.1g))
    
    graph combine line box, iscale(0.5) ycommon
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	59.7 KB
ID:	1510851

    Comment


    • #3
      Thanks Andrew, that's very helpful.

      I didn't know you could break up labels, that's a nice trick. I also found that xscale(titlegap()) can be used to push the axis up and down a bit.

      Comment


      • #4
        Here's another take, using stripplot (SSC) to show dot-box plots.


        Code:
        separate ka, by(incgroup_th) veryshortlabel
        gen lastyear = ka if year == 2016
        line ka? year, ylabel(0(0.2)1, format(%2.1g)) legend(off) xscale(range(2022)) || ///
        scatter lastyear year, mlabel(incgroup_th) mcolor(none) name(line, replace) xtitle("") ytitle(whatever makes sense) 
        
        stripplot ka, over(incgroup_th) vertical name(box, replace) ylabel(0(0.2)1, format(%2.1g)) ///
        box xtitle("") ytitle("") 
        
        graph combine line box, iscale(0.5) ycommon
        Click image for larger version

Name:	linebox.png
Views:	1
Size:	67.0 KB
ID:	1510881

        Comment


        • #5
          Thank you Nick, that works nicely indeed.

          Comment

          Working...
          X