Announcement

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

  • Overlay saved graphs

    Hi, I am trying to overlay two graphs but one of the graphs is a saved graph on my computer. I do not have both graphs created on the same .do file for organization purposes and the time it would take to run both programs together. Is there a way to do this? Here is an example of my code:

    I have graph:

    line pcdist1_m_s wtper1_m_s year if tag_graph ==1 & sex ==1 , sort ///
    title("Living Independently") , ///
    subtitle("In School Percentage (%)")
    graph save "school1" , replace

    And I am trying to overlay "bar1.gph" from another folder. bar1.gph is this graph:

    graph bar pcdist1_m wtper1_m if tag_graph ==1 & sex ==1 , over (year)
    graph save "bar1" , replace

    I can provide more information if required

    Thank you

  • #2
    If you have both GPH files, you can extract the data and plot as in #6 of https://www.statalist.org/forums/for...mbomarginsplot.

    Comment


    • #3
      Thanks Andrew, I am trying to follow but having some issues. Would you be able to replicate the code for my graphs, which I have attached?
      Attached Files

      Comment


      • #4
        I don't get what you are asking here. graph bar and twoway line are not compatible. What you should do is get at the data again which means whatever is shown by

        Code:
        list pcdist1_m_s wtper1_m_s year if tag_graph ==1 & sex ==1
        and then use twoway to create a graph with the elements you wish. What Andrew Musau is suggesting won't be easier than that.

        Comment


        • #5
          It might be easier for me to explain what I am trying to accomplish, sorry for the confusion. I want to have the information in bar1.gph in the same graph as the information in school1.gph. So, for every year there would be a point on the line graph of school1 and two bars from bar1. The y-axis information for bar1 would be on the right ideally. Let me know if this makes sense and if it's doable. I can provide any more applicable information, too.

          Comment


          • #6
            Same answer from me. Show us the data if you want code suggestions.

            Comment


            • #7
              Here are the data appended. I do not know what variable is what between the line and graph, but here is a start. You can get rid of the legend if you wish by placing the variable names within the graph. Using a common y-axis will not show the variability in the lines, so I use two y-axes. Not ideal.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float(pcdist1_m_s wtper1_m_s year) str4 which double _values long _variables float time
               22.15547 20.850925 2000 "line"                  . .  1
              22.331984 20.489134 2001 "line"                  . .  4
              23.552557 21.844833 2002 "line"                  . .  7
                23.2848 20.969044 2003 "line"                  . . 10
              24.216354  21.44417 2004 "line"                  . . 13
              21.951176  21.56053 2005 "line"                  . . 16
              21.098444 20.660835 2006 "line"                  . . 19
               21.22517 20.605463 2007 "line"                  . . 22
              21.789783 21.437635 2008 "line"                  . . 25
               22.29958   22.1657 2009 "line"                  . . 28
              22.939276 23.232124 2010 "line"                  . . 31
              23.363186 23.852654 2011 "line"                  . . 34
               22.92862  23.42985 2012 "line"                  . . 37
              22.751297 22.587463 2013 "line"                  . . 40
               22.49143 22.713467 2014 "line"                  . . 43
                21.8819  22.25172 2015 "line"                  . . 46
               21.57148 21.704336 2016 "line"                  . . 49
              21.200323 20.906786 2017 "line"                  . . 52
              20.336197 20.333696 2018 "line"                  . . 55
               19.81059  19.61504 2019 "line"                  . . 58
                      .         . 2000 "bar"   59.58938980102539 1  1
                      .         . 2000 "bar"  62.615535736083984 2  2
                      .         . 2001 "bar"  59.164512634277344 1  4
                      .         . 2001 "bar"  62.447540283203125 2  5
                      .         . 2002 "bar"   57.92429733276367 1  7
                      .         . 2002 "bar"  61.936771392822266 2  8
                      .         . 2003 "bar"   56.66088104248047 1 10
                      .         . 2003 "bar"  60.569358825683594 2 11
                      .         . 2004 "bar"   55.76005172729492 1 13
                      .         . 2004 "bar"  59.977725982666016 2 14
                      .         . 2005 "bar"   54.33139419555664 1 16
                      .         . 2005 "bar"  60.651241302490234 2 17
                      .         . 2006 "bar"   54.80697250366211 1 19
                      .         . 2006 "bar"   57.76509094238281 2 20
                      .         . 2007 "bar"   54.11357879638672 1 22
                      .         . 2007 "bar"   57.63212966918945 2 23
                      .         . 2008 "bar"  54.109012603759766 1 25
                      .         . 2008 "bar"  56.766902923583984 2 26
                      .         . 2009 "bar"   52.36817932128906 1 28
                      .         . 2009 "bar"   55.83025360107422 2 29
                      .         . 2010 "bar"   51.96544647216797 1 31
                      .         . 2010 "bar"   55.13921356201172 2 32
                      .         . 2011 "bar"   49.85455322265625 1 34
                      .         . 2011 "bar"   52.83445358276367 2 35
                      .         . 2012 "bar"  49.237510681152344 1 37
                      .         . 2012 "bar"    52.0350227355957 2 38
                      .         . 2013 "bar"   49.85945129394531 1 40
                      .         . 2013 "bar"   51.70431137084961 2 41
                      .         . 2014 "bar"  49.882606506347656 1 43
                      .         . 2014 "bar"    51.0304069519043 2 44
                      .         . 2015 "bar"  50.172088623046875 1 46
                      .         . 2015 "bar"  50.744956970214844 2 47
                      .         . 2016 "bar"   50.39216995239258 1 49
                      .         . 2016 "bar"  50.568626403808594 2 50
                      .         . 2017 "bar"   51.01835632324219 1 52
                      .         . 2017 "bar"     50.493896484375 2 53
                      .         . 2018 "bar"   50.88207244873047 1 55
                      .         . 2018 "bar"  50.687774658203125 2 56
                      .         . 2019 "bar"   50.76255416870117 1 58
                      .         . 2019 "bar"   50.92477035522461 2 59
              end
              label values time time
              label def time 1 "2000", modify
              label def time 2 "2000", modify
              label def time 4 "2001", modify
              label def time 5 "2001", modify
              label def time 7 "2002", modify
              label def time 8 "2002", modify
              label def time 10 "2003", modify
              label def time 11 "2003", modify
              label def time 13 "2004", modify
              label def time 14 "2004", modify
              label def time 16 "2005", modify
              label def time 17 "2005", modify
              label def time 19 "2006", modify
              label def time 20 "2006", modify
              label def time 22 "2007", modify
              label def time 23 "2007", modify
              label def time 25 "2008", modify
              label def time 26 "2008", modify
              label def time 28 "2009", modify
              label def time 29 "2009", modify
              label def time 31 "2010", modify
              label def time 32 "2010", modify
              label def time 34 "2011", modify
              label def time 35 "2011", modify
              label def time 37 "2012", modify
              label def time 38 "2012", modify
              label def time 40 "2013", modify
              label def time 41 "2013", modify
              label def time 43 "2014", modify
              label def time 44 "2014", modify
              label def time 46 "2015", modify
              label def time 47 "2015", modify
              label def time 49 "2016", modify
              label def time 50 "2016", modify
              label def time 52 "2017", modify
              label def time 53 "2017", modify
              label def time 55 "2018", modify
              label def time 56 "2018", modify
              label def time 58 "2019", modify
              label def time 59 "2019", modify
              
              tw (bar _values time if mod(_variables, 2), bc(red%50) xtitle("") ///
              ytitle("Some description (bars)") ylab(0(10)70)) (bar _values time if !mod(_variables, 2), ///
              bc(blue%50) xlab(2(3)59, val angle(vert) notick)) (line pcdist1_m_s ///
              wtper1_m_s time, yaxis(2) lc(red blue) leg(order(3 4)) ///
              ytitle("Some other description (lines)", axis(2)) plotregion(margin(zero)))
              Click image for larger version

Name:	Graph.png
Views:	1
Size:	74.3 KB
ID:	1693892

              Last edited by Andrew Musau; 16 Dec 2022, 15:13.

              Comment


              • #8
                Now that Andrew Musau has done the hard work, other suggestions are possible. The choice of bars for one variable and the use of different scales are not to me obviously good choices.

                With the data in #7 I got this.

                Code:
                line _values year if mod(_variables, 2), lc(black) xtitle("") ///
                ytitle("Some description")  xsc(off) name(G1, replace) fysize(45)
                
                line pcdist1_m_s wtper1_m_s year, lc(red blue) ///
                ytitle("% living independently in school") xtitle("") legend(order(1 "unweighted" 2 "weighted")ring(0) pos(1) col(1)) name(G2, replace) fysize(55)
                
                graph combine G1 G2, xcommon col(1)
                Why one variable is around 55 or so and the others much lower I can't follow. No doubt there is a story.


                Click image for larger version

Name:	composite2.png
Views:	1
Size:	29.8 KB
ID:	1693929

                Comment


                • #9
                  Thank you both this has been very helpful.

                  Andrew, that is what I was looking for and Nick, I agree that I might have to reformat but I should be all set from here. Thanks again

                  Comment

                  Working...
                  X