Announcement

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

  • Combine two plots (on top of each other)

    Hey,

    I would like to combine two line plots (see attachment). The top part shows four different series. The black series is the benchmark and I would like to "highlight" the distance between this series and each of the other ones. In order to do that I would like to show the distance in a separate plot below. Is it possible to do this in Stata directly? The attached graph was merged manually.

    Thanks in advance!

    best,
    Thomas

    Click image for larger version

Name:	graph_combined_anonymous.png
Views:	1
Size:	220.7 KB
ID:	1452076

  • #2
    Please check out points at FAQ Advice #12. Show .png files, not something else.

    The problem is a little tricky but here is some technique. To get a good pair of graphs, you need a different data structure. graph combine is great, but you can only go so far with it.

    I will be travelling for a couple of days and unlikely to be able to answer any questions in that period.

    Code:
    webuse grunfeld, clear 
    gen Yraw = log(invest) 
    keep if company < 5 
    
    egen reference = total((company == 4) * Yraw) , by(year) 
    
    gen Ydiff = Yraw - reference if company < 4 
    
    keep Y* company year 
    
    list 
    
    reshape long Y, i(company year) j(which) string 
    
    separate Y, by(company) veryshortlabel 
    
    su year, meanonly 
    local last = r(max) 
    gen show = company if year == `last'
    
    label define which 1 raw 2 diff 
    encode which, gen(panel) label(which) 
    
    quietly tab company
    local SHOW = r(r) * "show " 
    
    line Y? year , by(panel, col(1) note("") yrescale legend(off)) ///
    subtitle(, pos(9) nobox nobexpand fcolor(none)) ///
     || scatter Y? year if year == `last', ms(none ..) mla(`SHOW') mlabpos(3 ..)
    Click image for larger version

Name:	rawanddiff.png
Views:	1
Size:	33.5 KB
ID:	1452155

    Comment


    • #3
      Thanks for your answer and sorry for the late reply! I'll have a look at your code and probably ask some questions afterwards.

      Btw, I added the image as png?

      edit:
      It's not urgent!

      Comment


      • #4
        I had a look at your code. I'm currently not sure whether it's possible to combine the code I use(d) with the by-option.

        The attached image shows how I'd like the result to look like (obviously the x-axes should match). I changed a few things compared to my first post (mainly the shaded areas).

        While it's not the nicest way to get the intended result it might be easier to create two plots and just stick the images together manually. In this case there's a different question. Why do the x-axes of my plots don't match and how can I change that?

        I used the following commands to get the two images:

        twoway ///
        function y=1.3, range(194 211) recast(area) color(gs12) base(0) || ///
        function y=1, range(194 211) lstyle(grid) || ///
        function y=0.5, range(194 211) lstyle(grid) || ///
        function y=yq(2010,1), range(0 1.3) horizontal lstyle(grid) || ///
        tsline benchmark_series series1 series2 series3 series4 if tin(1995q1,2011q2), lcolor(black cranberry lavender orange teal) xlabel(140 215, format(%tq)) ylabel(0.0(0.5)1.3) ///
        legend(order(5 6 7 8 9)) tlabel(1995q1(20)2011q2,grid) scheme(s2color)

        twoway ///
        function y=0.4, range(194 211) recast(area) color(gs12) base(-0.2) || ///
        function y=0.2, range(194 211) lstyle(grid) || ///
        function y=yq(2010,1), range(-0.2 0.4) horizontal lstyle(grid) || ///
        function y=0, range(140 215) lstyle(foreground) lcolor(black) || ///
        tsline series1_diff series2_diff series3_diff series4_diff if tin(1995q1,2011q2), lcolor(cranberry lavender orange teal) xlabel(140 215, format(%tq)) ///
        legend(order(5 6 7 8)) tlabel(1995q1(20)2011q2,grid) scheme(s2color)
        Click image for larger version

Name:	graph_combined1_anonymous.png
Views:	1
Size:	174.7 KB
ID:	1452539

        Comment


        • #5
          I found the problem with the x-axes (mistake in the code)!

          Comment


          • #6
            Actually that was wrong, but I solved the problem in the meantime. If one wants to draw a line at e.g. y = 0, then this has to be done via yline and not via a separate "function y = 0...".

            Comment

            Working...
            X