Announcement

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

  • two-way graph by panel ID and title of second y-axis

    Hej,
    I would like to plot a line graph by panel ID with two different y-variables, but I cannot display the second (right-handside) ytitle.
    Does anybody know how I can display the ytitle of the second y-axis (right-scale) after using the by-Option?
    Here is my command:

    Code:
    graph twoway     line factor1 year, sort lpatt(solid)
                ||  line factor2 year, sort lpatt(shortdash)  yaxis(2)
                     by(cname,     ylabel(#5, nogrid labsize(small))
                                ylabel(#4, nogrid labsize(small) axis(2))
                                ytitle("factor1 (right-scale)")
                                ytitle("factor2 (right-scale)", axis(2))
                                xtitle("year")
    Thanks a lot in advance,
    Steve

  • #2
    You have a syntax error in your example and didn't provide any data/reference any data that anyone else could use to replicate your issue. That said, it is likely an issue with not passing the iytitles option to the by option:

    Code:
    // Load example data
    sysuse uslifeexp, clear
    
    // Rename variables for reshaping
    rename (le_wmale le_wfemale le_bmale le_bfemale)(le1 le3 le2 le4)
    
    // For a second plot to show differences over life expectancies
    g diff1 = le_w - le_b
    g diff3 = le_b - le_w
    g diff2 = le_male - le_female
    g diff4 = le_female - le_male
    
    // Keep the variables of interest
    keep year le? diff?
    
    // Restructure the data
    reshape long le diff, i(year) j(group)
    
    // Create group Indicators
    g female = inlist(group, 2, 4)
    g black = inlist(group, 1, 3)
    
    // Add value labels to identify the panels in the bygraph
    la def female 0 "Males" 1 "Females"
    la def black 0 "White" 1 "Black/African American"
    
    // Apply the value labels
    la val female female
    la val black black
    
    tw line le year, sort lc(blue) yti("Life expectancy") ylab(#10, nogrid) ||   ///   
    line diff year, yaxis(2) xaxis(1) sort lc(orange) by(female, iytitles)         ///   
    yti("Difference between comparable groups", axis(2)) xti("Year")              ///   
    ylab(#5, nogrid labsize(vsmall) axis(2))

    Comment


    • #3
      Thanks for your quick reply and the effort to generate a dataset to replicate my issue.
      Since my panel identifier covers more than a hundred countries using the by-option leads to a graph with more than 10x10 subgraphs.
      Unfortunately, then using iytitles puts the label of the right/second y-axis title at each subgraph. However, I just want it to appear once at the right margin similar to the first y-axis title at the left.
      But, I found another solution by specifying the r1title-Option, but importantly specified within the by-Option

      Code:
      graph twoway    line factor1 year, sort lpatt(solid)
                  ||  line factor2 year, sort lpatt(shortdash)  yaxis(2)
                              by(cname,ylabel(#5, nogrid labsize(small) r1title("factor2 (right-scale)", axis(2)))
                              ylabel(#4, nogrid labsize(small) axis(2))
                              ytitle("factor1 (right-scale)")
                              xtitle("year")
      Best,
      Steve

      Comment

      Working...
      X