Announcement

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

  • Y-axis tick marks not aligning when using two y-axes in tw line

    First time poster here, so please bear with me. I'm using this data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long beafips str48 countyname double emp int(year naics) float(total_emp total_local_g)
    18043 "Floyd"  851 2001 541 26356  14874.08
    18043 "Floyd"  774 2001 332 26356  14874.08
    18043 "Floyd" 1632 2001 326 26356  14874.08
    18043 "Floyd" 1366 2002 326 26225 15591.977
    18043 "Floyd"  745 2002 332 26225 15591.977
    18043 "Floyd"  843 2002 541 26225 15591.977
    18043 "Floyd"  666 2003 332 27127 16298.873
    18043 "Floyd"  936 2003 541 27127 16298.873
    18043 "Floyd" 1209 2003 326 27127 16298.873
    18043 "Floyd" 1149 2004 326 28075 17058.186
    18043 "Floyd"  921 2004 541 28075 17058.186
    18043 "Floyd"  635 2004 332 28075 17058.186
    18043 "Floyd"  940 2005 541 28699  17209.52
    18043 "Floyd"  716 2005 332 28699  17209.52
    18043 "Floyd" 1230 2005 326 28699  17209.52
    18043 "Floyd"  774 2006 332 29278 17852.584
    18043 "Floyd"  941 2006 541 29278 17852.584
    18043 "Floyd" 1171 2006 326 29278 17852.584
    18043 "Floyd" 1035 2007 326 29153 17654.504
    18043 "Floyd"  972 2007 332 29153 17654.504
    18043 "Floyd" 1012 2007 541 29153 17654.504
    18043 "Floyd" 1018 2008 326 29178 17539.613
    18043 "Floyd" 1053 2008 332 29178 17539.613
    18043 "Floyd" 1002 2008 541 29178 17539.613
    18043 "Floyd"  899 2009 332 27829 18919.979
    18043 "Floyd"  958 2009 541 27829 18919.979
    18043 "Floyd"  685 2009 326 27829 18919.979
    18043 "Floyd"  880 2010 332 28141 18009.676
    18043 "Floyd"  971 2010 541 28141 18009.676
    18043 "Floyd"  785 2010 326 28141 18009.676
    18043 "Floyd" 1025 2011 541 27980  17958.94
    18043 "Floyd"  831 2011 326 27980  17958.94
    18043 "Floyd"  884 2011 332 27980  17958.94
    18043 "Floyd" 1067 2012 541 27801 17802.873
    18043 "Floyd"  920 2012 332 27801 17802.873
    18043 "Floyd"  928 2012 326 27801 17802.873
    18043 "Floyd"  900 2013 332 28357  18206.61
    18043 "Floyd" 1068 2013 541 28357  18206.61
    18043 "Floyd" 1103 2013 326 28357  18206.61
    18043 "Floyd" 1062 2014 541 28121 17828.709
    18043 "Floyd" 1200 2014 326 28121 17828.709
    18043 "Floyd"  946 2014 332 28121 17828.709
    18043 "Floyd" 1274 2015 326 28939 18186.248
    18043 "Floyd" 1098 2015 541 28939 18186.248
    18043 "Floyd"  948 2015 332 28939 18186.248
    18043 "Floyd" 1150 2016 326 29406 18759.494
    18043 "Floyd" 1146 2016 541 29406 18759.494
    18043 "Floyd"  912 2016 332 29406 18759.494
    end
    format %ty year
    I'm trying to
    Code:
    graph tw line
    using two Y-axes because my three employment variables, emp, total_local_g, and total_emp are of different scales. When I make the graph using the following code:

    Code:
    graph tw ///
    (line emp year if beafips == 18043 & naics == 326, yaxis(1) ylabel(#3, axis(1))) ///
    (line emp year if beafips == 18043 & naics == 541, yaxis(1) ylabel(#3, axis(1))) ///
    (line emp year if beafips == 18043 & naics == 332, yaxis(1) ylabel(#3, axis(1))) ///
    (line total_local_g year if beafips == 18043, yaxis(2) ylabel(#3, axis(2))) ///
    (line total_emp year if beafips == 18043, yaxis(2) ylabel(#3, axis(2))), ///
    legend(label(1 "naics 326") label(2 "naics 541") label(3 "naics 332"))
    The graph turns out like this:
    Click image for larger version

Name:	dataex floyd.png
Views:	2
Size:	30.4 KB
ID:	1521647

    It doesn't look bad, but I'd like the axis ticks on the right Y-axis to line up with those on the left Y-axis. I tried to control the number of ticks using the
    Code:
    ylabel(#3)
    command but that didn't appear to work. How can I get the axis ticks on both axes to align with the grid?
    Attached Files

  • #2
    You say this is not bad. I will go no further than say I think it's hard work for the reader and that aligning the labels won't help.

    I suggest another way to plot these data. This needs installation of multiline from SSC as announced at https://www.statalist.org/forums/for...ailable-on-ssc

    Here I make a guess that "Local Govt" is implied for one variable. Evidently that and other small details are easy to tweak.

    Thanks for your data example. The code below depends on the example data being for just one value of beafips, whatever that is (no doubt as clear to many readers as "moraine" or "drumlin" is to me).

    Code:
    separate emp, by(naics) veryshortlabel
    label var total_emp "Total"
    label var total_local_g `" "Local" "Govt" "'
    set scheme s1color
    multiline total_emp total_local_g emp??? year, missing recast(connected) xtitle("") xla(2001(5)2016)
    Click image for larger version

Name:	zipper.png
Views:	1
Size:	39.5 KB
ID:	1521654

    Comment


    • #3
      Thank you, Nick, for the advice. beafips is merely the FIPS code, a geographic identifier which was unnecessary to include since only one geography is in the data example. The reasoning for why I wanted to get the axis ticks to line up is I wanted to use the very handsome scheme 538 by Daniel Bischof, and the graph looks quite ugly when the ticks aren't lined up:

      Install scheme:
      Code:
      ssc install g538schemes, replace all
      The graph:
      Code:
      graph tw ///
      (line emp year if beafips == 18043 & naics == 326, yaxis(1) ylabel(#3, format(%9.0gc) axis(1))) ///
      (line emp year if beafips == 18043 & naics == 541, yaxis(1) ylabel(#3, axis(1))) ///
      (line emp year if beafips == 18043 & naics == 332, yaxis(1) ylabel(#3, axis(1))) ///
      (line total_local_g year if beafips == 18043, lpattern(dash) lwidth(medthick) yaxis(2) ylabel(15000(5000)30000, format(%9.0gc) axis(2))) ///
      (line total_emp year if beafips == 18043, lpattern(dash) lwidth(medthick) yaxis(2) ylabel(15000(5000)30000, axis(2))), ///
      legend(label(1 "Plastics & Rubber Products mfg.") label(2 "Professional, Scientific" "& Technical") label(3 "Fabricated Metal Product mfg.") label(4 "Total Local Employment" "(right axis)") label(5 "Total Employment (right axis)") size(vsmall) position(6) col(2)) ///
      title("Floyd County, IN", position(12)) ///
      subtitle("Top 3 Traded, Local, & Total Employment", size(small)) ///
      xtitle("Year") ///
      ytitle("Employment", margin(medium)) ///
      scheme(538w)
      Click image for larger version

Name:	dataex floyd.png
Views:	2
Size:	31.8 KB
ID:	1521662



      I'll consider using your suggested plot but I wanted to be able to compare the lines more easily, which I think is better achieved using something like my version.
      Attached Files
      Last edited by Riley Zipper; 23 Oct 2019, 12:26.

      Comment


      • #4
        Understood.

        Personally I am queasy about different scales on two axes and queasier still about aligning labels. The exception for me is when there is genuine equivalence (e.g. Fahrenheit and Celsius temperatures on left and right).

        I have just sent in proof corrections for an article in Stata Journal 19(4) on avoiding spaghetti. multiline didn't feature (that's possibly for a sequel) but there is an section on losing axis titles like "year" and indeed legends too.

        Comment


        • #5
          It doesn't look bad, but I'd like the axis ticks on the right Y-axis to line up with those on the left Y-axis. I tried to control the number of ticks using the ...
          I think you introduce the problem by specifying your own options. Adding the following appears to solve it.

          Code:
          graph tw ///
          (line emp year if beafips == 18043 & naics == 326, yaxis(1) ylabel(#3, axis(1))) ///
          (line emp year if beafips == 18043 & naics == 541, yaxis(1) ylabel(#3, axis(1))) ///
          (line emp year if beafips == 18043 & naics == 332, yaxis(1) yscale(r(500 1800)) ylabel(#3, axis(1))) ///
          (line total_local_g year if beafips == 18043, yaxis(2) ylabel(#3, axis(2))) ///
          (line total_emp year if beafips == 18043, yaxis(2) ylabel(#3, axis(2))), ///
          legend(label(1 "naics 326") label(2 "naics 541") label(3 "naics 332"))

          Click image for larger version

Name:	Graph.png
Views:	1
Size:	43.1 KB
ID:	1521697




          PS. The interesting story is to find out what caused the decline in plastic and rubber manufacturing in the 2000s. That's the whole ballgame... everything else looks pretty stable.

          Comment


          • #6
            Thank you, Andrew. I hadn't tried playing around with the yscale(r( )) option. Indeed, Plastics & Rubber was hit quite hard by the recession in this county and has not recovered.

            Comment

            Working...
            X