Announcement

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

  • Ticks alignment in a graph with two y-axes

    Dear all,

    I am writing here because I need some help to fine tune a graph's aesthetics.
    My graph uses two y-axes which have different scales. On the LHS I use a 0(.5)2.5 scale regarding a bar chart, while on the RHS I use a 0(.2)1 concerning a scatterplot, the latter being superimposed to the bar chart.

    The two y-axes have the same number of ticks. However, they are not perfectly aligned in that, when I use grid lines for the LHS y-axis, they do not correspond (although slightly, yet visibly) with the RHS y-axis ticks.

    Here's the code I am using: twoway bar avg_GH n_country, barw(.7) color(emidblue) xlabel( ang(45) labsize(vsmall)) xtitle("") ytitle("titleLHS" , size(small) axis(1)) yscale(titlegap(3) axis(1)) yla(0(.5)2.5, axis(1)) yaxis(1) ///
    || scatter avg_PH avg_MH n_country, msymbol(O D) msize(small small) yscale(titlegap(3) axis(2)) yla(0(.2)1, axis(2)) yaxis(2) ytitle("titleRHS", size(small) axis(2)) legend(rows(1) order(1 2 3)).

    Any ideas on how to fix this would be greatly appreciated. Thanks in advance!




  • #2
    Can you provide a reproducible example using a stock dataset (like webuse auto.dta, or seehttp://www.stata-press.com/data/r9/d.html for some common Stata manual datasets)? It is much easier to help you find a solution if we have a toy example to play with.
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Dear Carole,

      Thanks in advance for your help. Below there is an attempt to reproduce the issue using auto.dta.

      Code:
       webuse auto.dta, clear
        
        encode make, gen(model)
        
        gsort -price
        g n_make=_n
        list price make n_make
        
        local lab
        forvalues i=1/`=_N' {
        local lab `lab' `i' "`=make[`i']'"
        }
        twoway bar price n_make, barw(.7) color(emidblue) xlabel(`lab', ang(45) labsize(tiny)) xtitle("Make") yla(0(5000)15000, axis(1)) yaxis(1) || scatter mpg n_make, msymbol(O) msize(small) yla(10(10)40, axis(2)) yaxis(2)
      While the descending-order bar chart is much like mine, mpg is a very different variable from my 0-1 continuous indicators. However, the point here is different; namely, that despite having the same number of ticks on both vertical axes, the grid lines departing from the LHS one do not match with the ticks on the RHS one. I hope you can help me. Thanks!

      Comment

      Working...
      X