Announcement

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

  • Overlay separate panel data line plots rather than combine

    Hi, I have generated two separate panel data line plots in two different figures (by using the xtline function). I want to overlay these two separate line plots rather than combine them. I have not yet found a code that can do this. I have only found an overlay option for twoway graphs. I know that you can overlay many panels in one graph when making a panel data line plot, but the two plots I want to overlay are using two somewhat different datasets, so I think it would be tricky.

    I am looking for something like the graph combine code, where you type in the name of the graphs you want to combine.

    Thank you.

  • #2
    See the -overlay- option of xtline.

    Code:
    xtline var1 var2, overlay

    I know that you can overlay many panels in one graph when making a panel data line plot, but the two plots I want to overlay are using two somewhat different datasets, so I think it would be tricky.
    Are the scales the same? Then append the datasets.

    Comment


    • #3
      Thanks for your reply Andrew Musau. I see that I maybe was a bit unclear regarding the problem. To clarify, my dataset looks like this:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(date TRUST_high countryid) double GDP_percapita
      228 1 1 49026.2
      229 1 1 49191.9
      230 1 1 49660.5
      231 1 1 50076.5
      232 1 1 50679.1
      233 1 1 51495.4
      234 1 1 51723.3
      235 1 1 51882.7
      236 1 1   52021
      237 1 1 52665.9
      238 1 1 53229.4
      239 1 1 53712.4
      240 1 1 53975.9
      241 1 1 50325.9
      242 1 1 52712.8
      243 1 1 54728.8
      244 1 1 56222.2
      245 1 1 57434.6
      228 1 2 53649.2
      229 1 2 53855.6
      230 1 2 54274.4
      231 1 2 55088.1
      232 1 2 56136.4
      233 1 2 56950.7
      234 1 2 57103.1
      235 1 2 57714.9
      236 1 2   57951
      237 1 2 57788.3
      238 1 2   58109
      239 1 2 58406.4
      240 1 2 57611.9
      241 1 2 51087.8
      242 1 2 57265.1
      243 1 2   56436
      244 1 2 56654.7
      245 1 2 59708.1
      246 1 2 62434.1
      228 1 3 49990.7
      229 1 3 50198.8
      230 1 3 50398.4
      231 1 3 51191.4
      232 1 3 51723.7
      233 1 3 52406.6
      234 1 3 52685.7
      235 1 3 53294.4
      236 1 3 53349.2
      237 1 3 53823.8
      238 1 3 54498.9
      239 1 3 55411.4
      240 1 3   54338
      241 1 3 48232.5
      242 1 3 54713.8
      243 1 3 55049.6
      244 1 3 56314.9
      245 1 3   58147
      246 1 3 60109.1
      228 1 4 47831.2
      229 1 4 48261.6
      230 1 4 48489.5
      231 1 4 48947.6
      232 1 4 49694.6
      233 1 4 50331.3
      234 1 4 50331.4
      235 1 4 49947.8
      236 1 4 49373.4
      237 1 4 49532.3
      238 1 4 49305.1
      239 1 4 49301.3
      240 1 4 48290.8
      241 1 4   42721
      242 1 4 47049.5
      243 1 4 48394.3
      244 1 4 49554.3
      245 1 4 49799.2
      246 1 4 51074.4
      228 1 5 68623.6
      229 1 5 68642.8
      230 1 5 69006.8
      231 1 5 70062.7
      232 1 5 71151.2
      233 1 5 72151.5
      234 1 5 71736.2
      235 1 5   71675
      236 1 5 71408.4
      237 1 5 71728.1
      238 1 5 72184.6
      239 1 5 72962.1
      240 1 5 72526.9
      241 1 5 67972.3
      242 1 5 72966.8
      243 1 5 73158.3
      244 1 5 73638.9
      245 1 5 75891.5
      246 1 5   78268
      228 0 6 23711.3
      229 0 6 24299.5
      230 0 6 24934.1
      231 0 6 25196.3
      232 0 6 25546.4
      233 0 6 25915.2
      end
      format %tq date
      where TRUST_high indicates if the country is defined as a high-trust or low-trust country.

      Then I collapse the dataset and define the panel variable:

      Code:
      collapse (mean) GDP_percapita, by (TRUST_high date)
      
      xtset TRUST_high date
      and then I generate the first plot

      Code:
      xtline GDP_percapita, overlay plot1(lc(black) lpattern(dash)) plot2(lc(black)) legend(order(1 "Low-trust countries"2 "High-trust countries"))
      name(g1)
      which overlays all GDPpercapita line plots for each specific country and generate two lines, one for each panel (high/low trust countries).

      and then I generate the second plot, which is fairly similar, but the TRUST_high variable is slightly tweaked in terms of which countries are being defined as high- or low-trust:

      Code:
      xtline GDP_percapita, overlay plot1(lc(black) lpattern(dash)) plot2(lc(black)) legend(order(1 "Low-trust countries"2 "High-trust countries"))
      name(g2)
      So, the GDP_percapita dataset is the same in both graphs, but the distribution of high- and low-trust countries is what is different between the two graphs. Hence, I do not think the code you sent would work, as it assumes that there are two different dependent variables. Maybe the way around this "issue" is obvious, but I cannot see it as of now.
      Last edited by Erlend Sunde; 31 May 2022, 13:40.

      Comment


      • #4
        The expand command below mimics appending. "new" is an indicator for the appended dataset.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(date TRUST_high countryid) double GDP_percapita
        228 1 1 49026.2
        229 1 1 49191.9
        230 1 1 49660.5
        231 1 1 50076.5
        232 1 1 50679.1
        233 1 1 51495.4
        234 1 1 51723.3
        235 1 1 51882.7
        236 1 1   52021
        237 1 1 52665.9
        238 1 1 53229.4
        239 1 1 53712.4
        240 1 1 53975.9
        241 1 1 50325.9
        242 1 1 52712.8
        243 1 1 54728.8
        244 1 1 56222.2
        245 1 1 57434.6
        228 1 2 53649.2
        229 1 2 53855.6
        230 1 2 54274.4
        231 1 2 55088.1
        232 1 2 56136.4
        233 1 2 56950.7
        234 1 2 57103.1
        235 1 2 57714.9
        236 1 2   57951
        237 1 2 57788.3
        238 1 2   58109
        239 1 2 58406.4
        240 1 2 57611.9
        241 1 2 51087.8
        242 1 2 57265.1
        243 1 2   56436
        244 1 2 56654.7
        245 1 2 59708.1
        246 1 2 62434.1
        228 1 3 49990.7
        229 1 3 50198.8
        230 1 3 50398.4
        231 1 3 51191.4
        232 1 3 51723.7
        233 1 3 52406.6
        234 1 3 52685.7
        235 1 3 53294.4
        236 1 3 53349.2
        237 1 3 53823.8
        238 1 3 54498.9
        239 1 3 55411.4
        240 1 3   54338
        241 1 3 48232.5
        242 1 3 54713.8
        243 1 3 55049.6
        244 1 3 56314.9
        245 1 3   58147
        246 1 3 60109.1
        228 1 4 47831.2
        229 1 4 48261.6
        230 1 4 48489.5
        231 1 4 48947.6
        232 1 4 49694.6
        233 1 4 50331.3
        234 1 4 50331.4
        235 1 4 49947.8
        236 1 4 49373.4
        237 1 4 49532.3
        238 1 4 49305.1
        239 1 4 49301.3
        240 1 4 48290.8
        241 1 4   42721
        242 1 4 47049.5
        243 1 4 48394.3
        244 1 4 49554.3
        245 1 4 49799.2
        246 1 4 51074.4
        228 1 5 68623.6
        229 1 5 68642.8
        230 1 5 69006.8
        231 1 5 70062.7
        232 1 5 71151.2
        233 1 5 72151.5
        234 1 5 71736.2
        235 1 5   71675
        236 1 5 71408.4
        237 1 5 71728.1
        238 1 5 72184.6
        239 1 5 72962.1
        240 1 5 72526.9
        241 1 5 67972.3
        242 1 5 72966.8
        243 1 5 73158.3
        244 1 5 73638.9
        245 1 5 75891.5
        246 1 5   78268
        228 0 6 23711.3
        229 0 6 24299.5
        230 0 6 24934.1
        231 0 6 25196.3
        232 0 6 25546.4
        233 0 6 25915.2
        end
        format %tq date
        
        expand 2, g(new)
        set seed 05312022
        replace TRUST_high= runiformint(0,1) if new
        egen group= group(TRUST_high new)
        collapse GDP_percapita, by (group date)
        xtset group date
        xtline GDP, overlay
        Last edited by Andrew Musau; 31 May 2022, 14:32.

        Comment


        • #5
          Thank you Andrew Musau. It seems like that code just generates a new TRUST_high variable, but it does not show how to overlay the graph using the first TRUST_high variable with the other (tweaked) TRUST_high variable (lets call it TRUST_high2). Essentially, what I have is two different panel variables (TRUST_high and TRUST_high2). I have made two different separate plots using each of these panel variables, and I want to overlay these two plots.

          To clarify I can upload the graphs here. My first plot, using the panel variable TRUST_high, looks like this:

          Click image for larger version

Name:	Skjermbilde 2022-06-01 kl. 10.46.41.png
Views:	1
Size:	96.2 KB
ID:	1667184
          wwhile the second plot, using the panel variable TRUST_high2, looks similar to the graph above, but with some tweaks in levels, trends etc.

          Comment


          • #6
            We are going round in circles here. The idea is to create a categorical variable with 4 levels in the appended dataset, and this will give you what you want. Provide a data example including TRUST_high and TRUST_high2 if you are not able to implement the advice in #4.

            Comment


            • #7
              It worked! Sorry, I did not quite understand the code at first, but eventually it made sense. Thank you Andrew Musau

              Comment

              Working...
              X