Announcement

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

  • Plot multiple bar charts on one graph

    Hello,

    I am trying to plot multiple graphs onto one set of axis.

    I have one binary variable: OUTCOME (yes, no)
    I have four categorical variables: GREEN (1,2,3), PINK (1,2,3), RED(1,2,3), BLUE (1,2,3)

    I have created the following graphs to show the proportions of each category in variable GREEN for each OUTCOME.


    graph hbar if OUTCOME == 1, over(GREEN) asyvars stack blabel(bar, format(%2.0f))
    graph hbar if OUTCOME == 0, over(GREEN) asyvars stack blabel(bar, format(%2.0f))

    I would like these on one set of axis, along with similar pairs of plots for variables PINK, RED and BLUE. So in total there would be 8 bar charts on one graph.

    I have tried using tabplot, graph combine and graph twoway commands but I have had no success.

    Could you suggest how I combine all these plots onto one graph?

    Thank you very much

    Seren

  • #2
    Is this what you want? No data example here, so I had to make one up.

    tabplot is from the Stata Jourrnal.

    Code:
    clear
    set obs 100
    set seed 31459265
    set scheme s1color
    
    gen outcome = runiformint(0, 1)
    
    matrix p1 = (0.4, 0.3, 0.2, 0.1)  
     
    local j = 1
    gen rndm = .
    foreach v in green pink red blue {
    
        replace rndm = runiform()
        gen `v' = cond(rndm < p1[1, `j'] , 1, cond(rndm < 0.5, 2, 3))
        local ++j
    }
    
    preserve
    
    stack green outcome pink outcome red outcome blue outcome, into(categorical outcome) clear
    label def _stack 1 green 2 pink 3 red 4 blue
    label value _stack _stack
    
    tabplot outcome categorical, by(_stack, note("")) showval separate(_stack) bar1(color(green)) bar2(color(pink)) bar3(color(red)) bar4(color(blue)) yreverse
    
    restore
    Click image for larger version

Name:	colouredtabplot.png
Views:	1
Size:	15.7 KB
ID:	1703330

    Last edited by Nick Cox; 24 Feb 2023, 10:54.

    Comment


    • #3
      Thank you very much, this was exactly the type of graph I was thinking of.
      Is there any option where the bars for outcomes 0 and 1 could be next to each other for each category 1, 2 and 3 instead of one bar set above the other?

      Thank you.
      Best regards

      Seren

      Comment


      • #4
        Swap the variables around.

        Code:
        Code:
         
         tabplot categorical outcome, by(_stack, note("")) showval separate(_stack) bar1(color(green)) bar2(color(pink)) bar3(color(red)) bar4(color(blue)) yreverse

        Comment


        • #5
          Another take on the problem: the line graph bar is different.

          Code:
          clear 
          set obs 100
          set seed 31459265
          set scheme s1color
          
          gen outcome = runiformint(0, 1)
          
          matrix p1 = (0.4, 0.3, 0.2, 0.1)  
           
          local j = 1
          gen rndm = .
          foreach v in green pink red blue {
          
              replace rndm = runiform()
              gen `v' = cond(rndm < p1[1, `j'] , 1, cond(rndm < 0.5, 2, 3))
              local ++j
          }
          
          preserve
          
          stack green outcome pink outcome red outcome blue outcome, into(categorical outcome) clear
          label def _stack 1 green 2 pink 3 red 4 blue
          label value _stack _stack
          
          graph bar (count), over(outcome) over(categorical)  by(_stack, note("")) asyvars /// 
          bar(1, color(blue)) bar(2, color(orange)) 
          
          restore

          Comment

          Working...
          X