Announcement

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

  • Problem creating a combined bar and line graph with two y-axes

    Hi,

    I am attempting to create a combined bar and line graph with two y-axes including a legend and colour format. I have struggled putting together code from my university sources for these graphs individually.

    The dataset I am working with is as follows:

    input int date double(sv tv svtv)
    2021 263265.01367681153 1884107.5491802376 .13972929188217326
    2020 160562.117150004 1247802.0562035448 .12867595172788582
    2019 176471.19955512544 1749111.8072808338 .10089189199944128
    2018 170093.77284257952 1804252.3167105296 .09427382814876496
    2017 52161.34900366189 1402845.4179146306 .03718253510868019
    2016 114215.2414812902 1684493.7934715918 .06780389570085785
    2015 80283.8120504003 1874024.0737563872 .042840331228763476
    2014 58058.12532963137 1554142.3946453538 .037357017947431966
    2013 40682.652479267206 1019954.9128099392 .03988671652866298
    2012 38677.2364842603 914746.105465388 .04228193621505805
    2011 57203.51218648255 967624.8487086741 .059117448526484664
    2010 35043.662845142186 837891.7568109324 .041823615712034835
    2009 29106.356725849208 656567.4335384262 .04433110026336043
    2008 19632.641050937582 889188.2822891638 .022079284491237878
    2007 108563.48645410615 1625505.757096285 .0667875127357519
    2006 45838.282629804686 1511020.1616175605 .030335983459502218
    2005 37117.18074119836 1183223.5713459835 .03136954134456219
    2004 31706.67899895087 889931.028397809 .035628243074111166
    2003 29608.788667924702 579999.2549263801 .051049701213293754
    2002 11840.867066330742 482330.77073760197 .02454926740050848
    2001 13351.644165197888 792923.65140603 .01683849906800289
    2000 55384.067105144255 1553569.4440576113 .03564955999681108

    I am trying to graph sv with a bar graph and svtv with a line graph, would anyone know how I can attempt this? Thanks in advance!

  • #2
    Such combinations are quite popular but in my experience hard to get right. You don't show the code you tried but some trickery includes

    * putting the line on top of the bar, not vice versa

    * matching colours of axis title and labels to what is shown in the plot region (harder if you are restricted on colours you can use).

    I also suggest trying multiline from SSC, as explained at https://www.statalist.org/forums/for...ailable-on-ssc

    Code:
    clear 
    input int date double(sv tv svtv)
    2021 263265.01367681153 1884107.5491802376 .13972929188217326
    2020 160562.117150004 1247802.0562035448 .12867595172788582
    2019 176471.19955512544 1749111.8072808338 .10089189199944128
    2018 170093.77284257952 1804252.3167105296 .09427382814876496
    2017 52161.34900366189 1402845.4179146306 .03718253510868019
    2016 114215.2414812902 1684493.7934715918 .06780389570085785
    2015 80283.8120504003 1874024.0737563872 .042840331228763476
    2014 58058.12532963137 1554142.3946453538 .037357017947431966
    2013 40682.652479267206 1019954.9128099392 .03988671652866298
    2012 38677.2364842603 914746.105465388 .04228193621505805
    2011 57203.51218648255 967624.8487086741 .059117448526484664
    2010 35043.662845142186 837891.7568109324 .041823615712034835
    2009 29106.356725849208 656567.4335384262 .04433110026336043
    2008 19632.641050937582 889188.2822891638 .022079284491237878
    2007 108563.48645410615 1625505.757096285 .0667875127357519
    2006 45838.282629804686 1511020.1616175605 .030335983459502218
    2005 37117.18074119836 1183223.5713459835 .03136954134456219
    2004 31706.67899895087 889931.028397809 .035628243074111166
    2003 29608.788667924702 579999.2549263801 .051049701213293754
    2002 11840.867066330742 482330.77073760197 .02454926740050848
    2001 13351.644165197888 792923.65140603 .01683849906800289
    2000 55384.067105144255 1553569.4440576113 .03564955999681108
    end 
    
    set scheme s1color 
    gen sv2 = sv/1000
    gen tv2 = tv/1000 
    label var sv2 "sv (000)"
    label var tv2 "tv (000)"
    
    local opts1 yaxis(1) yla(, ang(h) labcolor(black) axis(1)) fcolor(black*0.2) lcolor(black) ytitle(, color(black) axis(1))
    local opts2 yaxis(2) yla(, ang(h) labcolor(red) axis(2)) lw(medthick) lcolor(red) ytitle(, color(red) axis(2)) 
    
    
    twoway bar sv2 date, `opts1' || line tv2 date, `opts2' legend(off) xtitle("") name(G1, replace)
    
    multiline sv2 tv2 date , name(G2, replace) xtitle("") xla(, grid glw(thin)glc(gs8))
    Click image for larger version

Name:	barandline.png
Views:	1
Size:	41.8 KB
ID:	1677676
    Click image for larger version

Name:	multiline.png
Views:	1
Size:	33.5 KB
ID:	1677677

    Comment


    • #3
      You can go further with colouring axis line and tick marks.

      Comment


      • #4
        Hi Nick! Thank you so much for the help!

        Comment


        • #5
          It seems possible that plotting both on log scale would help and that recalling that log(sv / tv) = log sv - log tv would be instructive.

          Comment

          Working...
          X