Announcement

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

  • Two scales in a graph

    Dear Community,

    I want to show on the graph how two indicators evolved over time. The problem is the range of the first is from 50 to 250, whereas the range of the second indicator is from 0.15 to 0.17. I used the command:
    Code:
    twoway connected x1 x2 year,sort ///
    yaxis(1 2) ytitle("x1") ///
    ylabel(50(25)250, nogrid angle(horizontal) xscale(titlegap(3)) ///
    ylabel(, axis(2) angle(horizontal)) ///
    ylabel(0 "0" 0.05 "0.05" 0.1 "0.1" 0.15 "0.15" 0.2 "0.20", axis(2)) ///
    scale(0(0.5)0.20),axis(2))
    yscale(titlegap(3) axis(2)) yscale(titlegap(3) axis(1))
    However the output is not what I want. The axis with the values for x2 is illegible. The graph for x2 is flat. I would like to have different scale on the right horizontal axis so that the graph would show the fluctuations of x2


  • #2
    There are at least 3 problems here. Two are the lack of a data example and the fact that even with a data example your code won't run at all. Correcting that with say

    Code:
    clear 
    set obs 40
    gen year = 1979 + _n 
    range x1 50 250 
    range x2 0.15 0.17 
    
    twoway connected x1 x2 year,sort ///
    yaxis(1 2) ytitle("x1") ///
    ylabel(50(25)250, nogrid angle(horizontal) axis(1)) xscale(titlegap(3)) ///
    ylabel(, axis(2) angle(horizontal)) ///
    ylabel(0 "0" 0.05 "0.05" 0.1 "0.1" 0.15 "0.15" 0.2 "0.20", axis(2)) ///
    yscale(r(0 0.20) axis(2)) /// 
    yscale(titlegap(3) axis(2)) yscale(titlegap(3) axis(1))
    I can confirm the first problem, and it puzzles me too.

    I suggest that you would be better off with another design, using multiline from SSC for example. See https://www.statalist.org/forums/for...connected-plot for a recent example.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      I can confirm the first problem, and it puzzles me too.
      It appears that twoway constrains the axes to be the same with a single plot. You can split the connected plot into two plots.

      Code:
      clear 
      set obs 40
      gen year = 1979 + _n 
      range x1 50 250 
      range x2 0.15 0.17 
      set scheme s1color
      
      twoway connected x1 year, sort || connected x2 year, sort yaxis(2) ///
      ylabel(50(25)250, nogrid angle(horizontal) axis(1)) xscale(titlegap(3)) ///
      ylabel(, axis(2) angle(horizontal)) ///
      ylabel(0 "0" 0.05 "0.05" 0.1 "0.1" 0.15 "0.15" 0.2 "0.20", axis(2)) ///
      yscale(r(0 0.20) axis(2)) xtitle("") /// 
      yscale(titlegap(3) axis(2)) yscale(titlegap(3) axis(1))

      Click image for larger version

Name:	Graph.png
Views:	1
Size:	45.1 KB
ID:	1666082

      Last edited by Andrew Musau; 24 May 2022, 05:28.

      Comment


      • #4
        Andrew Musau is clearly on target, but I still commend a different design.

        Comment


        • #5
          I have my graph! Thank you so much Andrew and Nick for your kind help

          Comment

          Working...
          X