Announcement

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

  • How do I put a y-title on the 2nd axis when using by() as a twoway option?

    For example, the following code produces a graph with the mpg scale on the left y-axis and the price scale on the right y-axis. However, it does not print a y-title for the right-axis - only the left y-axis:

    Code:
     
    preserve
    sysuse auto, clear
    
    twoway lowess mpg weight || lowess price weight, yaxis(2) ||, by(foreign)
    
    restore



    The Stata help for axis_choice_options suggests that you need to specify the axis as an option under ytitle (see p. 3-4), like this:
    Code:
    ytitle("my 2nd y-axis title", axis(2))
    Page 6 of this PDF explicitly uses -ytitle(,axis(2))- as a twoway option.

    However, when I do this as either a twoway option or as an option to the 2nd graph, the y-title still does not appear:

    Code:
    preserve
    sysuse auto, clear
    
    *ytitle(,axis(2)) as a Twoway Option
    twoway lowess mpg weight || lowess price weight, yaxis(2) ||, by(foreign) ytitle("my 2nd y-axis title", axis(2))
    
    *ytitle(,axis(2)) as an Option to the Second Graph
    twoway lowess mpg weight || lowess price weight, yaxis(2) ytitle("my 2nd y-axis title", axis(2)) ||, by(foreign)
    
    restore
    And the result is the same whether I use -scatter-, -line-, etc.

    The only time the 2nd y-axis title will appear is when I remove the -by()- option:

    Code:
    twoway lowess mpg weight || lowess price weight, yaxis(2) ||, ytitle("my 2nd y-axis title", axis(2))


    Does anyone know how I can get the 2nd y-axis title when specifying the -by()- option?




  • #2
    Use the r1title() option within by()


    Code:
    sysuse auto, clear
    
    twoway lowess mpg weight  /// 
      || lowess price weight, yaxis(2)  ///
      ||, by(foreign , r1title("Price"))

    Comment


    • #3
      Using the r1title option within by() does work, but isn't this a bug? Or at least a syntax inconsistency? It seems odd that simply adding the by() option causes the syntax for the second y-axis title to change.

      Comment

      Working...
      X