Announcement

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

  • Creating a 2 Y-axis line using two datasets

    Hello Stata community;
    I have 2 Stata datasets: one is called Republicans, and the other one is Sunspots.
    Example of Republicans data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Year byte Number_Republicans
    1961 36
    1963 34
    1965 32
    1967 36
    1969 43
    1971 44
    1973 42
    1975 37
    1977 38
    1979 41
    end

    Example of Sunspots data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Year double Sunspots_Number
    1960 112.3
    1961  53.9
    1962  37.6
    1963  27.9
    1964  10.2
    1965  15.1
    1966    47
    1967  93.8
    1968 105.9
    1969 105.5
    1970 104.5
    1971  66.6
    1972  68.9
    1973    38
    1974  34.5
    1975  15.5
    1976  12.6
    1977  27.5
    1978  92.5
    1979 155.4
    1980 154.6
    end
    As you can see, both datasets have the same X axis (Year), but different Y axis (Republicans number for the first dataset, Sunspots number for the second), my goal is to draw a 2-line graph showing the evolution of Sunspots number and Republicans number. Since both of the datasets have the same X-axis, the idea of merging them comes to mind already, I wanna know if merging the 2 datasets is correct (I think it is), second, I would to get help on how to draw the 2-line grapgh, I would love to have the Sunspots number on the left Y axis, and the Republicans number on the right Y axis, and both of those Y variables don't have the same scale.

    Thanks very much for your help.

  • #2
    See https://www.statalist.org/forums/for...les-in-a-graph for a very recent discussion of this problem..


    Here's the solution you probably won't like, although in my view it's clearer than the alternative.

    Code:
    clear
    input int Year byte Number_Republicans
    1961 36
    1963 34
    1965 32
    1967 36
    1969 43
    1971 44
    1973 42
    1975 37
    1977 38
    1979 41
    end
    
    save DS1 , replace
    
    clear
    
    input int Year double Sunspots_Number
    1960 112.3
    1961  53.9
    1962  37.6
    1963  27.9
    1964  10.2
    1965  15.1
    1966    47
    1967  93.8
    1968 105.9
    1969 105.5
    1970 104.5
    1971  66.6
    1972  68.9
    1973    38
    1974  34.5
    1975  15.5
    1976  12.6
    1977  27.5
    1978  92.5
    1979 155.4
    1980 154.6
    end
    
    merge 1:1 Year using DS1
    
    set scheme s1color
    
    ssc install multiline
    
    foreach v of varlist N S {
        local label = subinstr("`v'", "_", " ", 1)
        label var `v' "`label'"
    }
    
    multiline N S Y, subtitle(, orient(vertical)) recast(connected) xla(, grid)
    
    
    set scheme s1color
    ssc install N S Y .
    multiline N S Y
    Click image for larger version

Name:	sunspots.png
Views:	1
Size:	35.8 KB
ID:	1666637



    Sunspots aren't causative. They are the result of embarrassment.
    Last edited by Nick Cox; 28 May 2022, 04:58.

    Comment


    • #3
      Dear Mr. Cox;

      Thanks for your help, I really appreciate it.
      What I wanna say is that: Yes, I know that the relationshio in question is kinda of a "false" or a spurious". My goal is to prove that with a graph in hand showing both events on the same graph, on the same X-Y axis (Well, here it is gonna be 2 Y axis), I don't know if it's just because both phenomenas aren't causal that you've suggested presenting them in two separate graphs, but my goal is to show both events on the same graph and to explain that, even if it looks like there is a correlation, the relationship is just not causal.
      What I wanna know is that: Could I / Should I present them in the same one graph ??

      Comment


      • #4
        I think the graph is clearer than the alternative you seem to have in mind, which isn't very different in principle. The reader has to work hard to work which variable is to be explained by the left axis and which variable by the right axis. They may be confused even if you aren't.

        Comment


        • #5
          Well, yeah you're kinda right about that Sir, but, again, it's kinda the goal of the study to show both lines in the same graph so that the reader could see that the two phenomenas show some kind of correlation. As for the axis, I would write their titles, and also, if you've notices, they don't have the same scale.
          Thanks very much for the help.

          Comment

          Working...
          X