Announcement

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

  • plotting multiple bar and line graphs

    I am trying to graph multiple graphs overlying on top of each other.
    Each graph needs to look at the sum of variable "patients" but I limit the number of patients I look at by a different variable.
    the x axis is variable "years".
    So basically I want to put these three graphs on top of each other where pyp_planar is the botton, pyp_spect is next and pyp_spectct is on top.

    graph bar (sum) patients if pyp_planar==1&state=="National", over(year) name (pyp_planar)
    graph save Graph "D:\MPOP_geography\mpop_pyp_planar.gph"
    graph bar (sum) patients if pyp_spect==1&state=="National", over(year) name (pyp_spect)
    graph save Graph "D:\MPOP_geography\mpop_pyp_spect.gph"
    graph bar (sum) patients if pyp_spectct==1&state=="National", over(year) name (pyp_spectct)
    graph save Graph "D:\MPOP_geography\mpop_pyp_spectct.gph"

    when I tried -graph combine- each graph was graphed separately. They were also not labeled so I am not able to tell which graph presents which data.

    I would also like to do the same thing as line graphs.

    Thanks for the help.

  • #2
    I'm not sure what you mean by "on top of each other". Do want to stack the bars?

    Comment


    • #3
      Consider this sample code:

      Code:
      sysuse nlsw88, clear
      
      gen hours_race = hours if race == 1
      gen hours_married = hours if married == 1
      gen hours_south = hours if south == 1
      
      collapse (sum) hours_*, by(grade)
      
      local other_opts legend(label(1 "Race = 1") label(2 "Married = 1") label(3 "South = 1") pos(10) ring(0) col(1)) ytitle(Total Hours)  scheme(s2color)
      
      graph bar hours_race hours_married hours_south, over(grade) stack `other_opts' name(bar, replace)
      twoway line hours_race hours_married hours_south grade, `other_opts' name(line, replace)
      This produces these two graphs:

      Click image for larger version

Name:	Screenshot 2022-09-14 at 11.06.55 PM.png
Views:	1
Size:	118.3 KB
ID:	1681971

      Click image for larger version

Name:	Screenshot 2022-09-14 at 11.07.08 PM.png
Views:	1
Size:	230.8 KB
ID:	1681972

      Comment


      • #4
        Thanks, this is very helpful. The problem with using -collapse- is that I lose the rest of my data and I want to do this for a bunch of variables. Is the better way for me to code for this to use -preserve- or can I ignore the -collapse- and get the same results? Thanks

        Comment


        • #5
          The bar graph can be made without collapsing your data and using (sum) in the graph command (like you had done). But the collapse is needed for the line graph.

          Comment


          • #6
            thanks.

            Comment

            Working...
            X