Announcement

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

  • Graph loops + Way of creating multiple graphs in same frame

    Hello,

    I am trying to plot three variables by state: relief per capita, damage per capita and percentage population affected data.

    I have done several combinations but broadly I am able to:
    (1) plot these together in one graph
    (2) plot them separately by each state within the same frame.

    However, I need to see nuanced version for each state.

    Hence, I am attempting to have a separate chart for each state.
    I need :
    1. Have two graphs side by side for each state separately – One for percentage of population affected and second a combined relief and damage per capita.
    2. A loop to make this work for all states (state variable is numeric)
    3. Export all of these in pdf
    Is this possible? Or any version of this which will make it easier to see each state as a chart rather than each variable by state.

    Any help would be appreciated.

    Thank you

  • #2
    Here is sample code that uses graph combine to combine two graphs into a single graph with two plots side-by-side and then exports the result as a PDF.
    Code:
    sysuse auto, clear
    twoway scatter price weight, name(g1)
    twoway scatter price length, name(g2)
    graph combine g1 g2, rows(1) name(gg)
    graph export "mygraph.pdf", name(gg) replace

    Comment


    • #3
      Hi William,

      thank you for the reply. Could you also suggest if it is possible to loop this for all values of state?

      regards

      Comment


      • #4
        The following is the general approach
        Code:
        levelsof state : local(states)
        foreach state of local states {
            twoway ... if state==`state' ... name(g1, replace) ...
            twoway ... if state==`state' ... name(g2, replace) ...
            graph combine g1 g2, name(gg), replace
            graph export "State`state'.pdf", name(gg)
        }
        This should give you a start toward a solution specific to your data and your needs. Let me add the following: for developing your code, pick two state numbers - say 6 and 27 - and replace the foreach command with
        Code:
        foreach state in 6 27 {

        Comment


        • #5
          Thank you William.

          I am trying to do the same using:

          foreach state in 1 5 {
          twoway (bar popaff year, sort), ytitle(Population affected (%)) xtitle(Year),if state==`state', name(g1,replace)
          twoway (bar flood_pc year, sort), ytitle(Damage per capita) xtitle(Year),if state==`state', name(g2,replace)
          graph combine g1 g2, name(gg), replace
          graph export "State`state'.pdf", name(gg)
          }


          However, this is giving me graphs one after the other. I am trying to get them in the same frame but different columns so I have one figure (with multiple sub-graphs) for each state i.e. one page of pdf has all graphs related to one state

          Also, could you please tell me how to name each graph with the actual state name? As mentioned earlier my state is a numeric variable from 1-30 and has labels attached to it as state names.

          Lastly, I am trying to save these using the graph export option but none of my files are getting saved. I would like all of these to be saved (appended) in one pdf file with state names as titles.

          My apologies if these are too many queries. But despite playing around with options, I cant seem to get them in a proper way.

          Thank you for all your help.

          Regards,
          Purnima




          Comment


          • #6
            When I work with Stata graphics, everything - and I mean everything - I do is trial and error reading the help files. I find it difficult and time-consuming. I have no ready answers to your questions about graphing - perhaps some other reader will.

            Regarding state names,
            Code:
            local statename : label (state) `state'
            will create the local macro statename containing the value label (state name) for the state variable value `state' . This local macro can then be used wherever you want the state name, for example
            Code:
            ... title("`statename'")

            Comment

            Working...
            X