Announcement

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

  • How to generate 2nd Y-axis for Having two connected line graphs on one graph

    I have used the following code to generate two seperate graphs. How do I combine the two graphs onto one by generating a 2nd Y-Axis as I'd like to show the relationship of the two line graphs together?

    egen meanCEOTotalCompensation = mean(CEOTotalCompensation), by(Year)
    egen meanShareholderReturn = mean(ShareholderReturn), by(Year)
    egen tag = tag(Year)
    twoway connected meanCEOTotalCompensation Year if tag, sort
    twoway connected meanShareholderReturn Year if tag, sort

    Any help would be greatly appreciated!

  • #2
    You don't show example data, so I'll illustrate the approach using one of Stata's on-line data sets. Also, for this application, it is probably simpler to use -collapse- than a series of -egen- commands and tagging individual years:

    Code:
    webuse nlswork, clear
    
    collapse (mean) ln_wage wks_work, by(year)
    
    graph twoway (connected ln_wage year, yaxis(1)) (connect wks_work year, yaxis(2))
    In the future, when asking for help with code, show data examples, and please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you very much Clyde. Much Appreciated

      Comment


      • #4
        I'm similarly trying to combine graphs of US and European data so they appear on the same graph. But one set is a regression and the second set is in a binned scatterplot format.

        1. The following code produces graphs in my desired format, but now I would like to make the US and European ones combine on the same graph.

        reg gdp_growth density50p ln_gdppc ln_popdensity if US==1 , robust cluster(region)
        avplot density50p

        reg gdp_growth density50p ln_gdppc ln_popdensity if euro==1 , robust cluster(region)
        avplot density50p


        I've tried using [graph combine] but it just makes these separate graphs sit next to each other. How do I put these data points and regression lines on the same graph for the US and Europe?

        2. Similarly, I have two binned scatterplots (measuring the relationship between firm density and GDP growth) that I'm trying to combine into one.

        binscatter gdp_growth density50p if US==1

        binscatter gdp_growth density50p if euro==1


        [Graph twoway scatter] combines normal scatterplots, but what commands should I use to maintain the binned scatterplot format?

        I appreciate any help! Also, apologies that I haven't used dataex. I installed it but couldn't figure out how to generate the code despite this helpful link.

        Comment

        Working...
        X