Announcement

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

  • Bar chart for two categorical variables with same value labels

    Hello! I have two categorical variables ("concerned_chemicals" and "concerned_bacteria") with the same label values (1="Not concerned", 2="Somewhat concerned", 3="Moderately concerned" 4="Very concerned")
    I would like to create a barplot showing the counts for each label for both variables overlaid onto the same graph. On the x-axis, I would like the four variable labels, and for each label, one bar showing the counts for "concerned_chemicals" and another bar next to it showing the counts for "concerned_bacteria".

    I've tried using catplot to do so, but when I use the command "catplot concerned_chemicals concerned_bacteria" or "catplot concerned_chemicals, over(concerned_bacteria)", it gives me a nested graph, which is not what I want.
    thank you for any input!

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(concerned_chemicals concerned_bacteria)
    2 2
    1 1
    4 2
    4 4
    1 1
    2 2
    1 1
    1 2
    1 3
    3 1
    1 1
    1 1
    4 2
    4 4
    2 1
    1 2
    3 1
    3 1
    1 1
    1 1
    4 1
    1 1
    2 3
    1 1
    1 1
    1 2
    1 1
    4 3
    4 4
    4 3
    3 2
    2 1
    3 3
    1 1
    2 1
    3 1
    1 1
    1 1
    3 1
    1 1
    1 1
    4 2
    1 1
    1 1
    1 1
    1 1
    1 1
    2 2
    1 1
    1 1
    1 2
    3 2
    3 3
    2 2
    1 1
    2 1
    1 1
    2 2
    3 1
    2 2
    2 2
    1 1
    2 1
    1 1
    2 2
    2 2
    2 2
    1 3
    2 1
    1 2
    1 1
    1 1
    1 1
    4 1
    2 2
    2 2
    1 1
    4 4
    2 2
    2 2
    1 1
    4 4
    1 3
    2 1
    3 1
    2 3
    4 1
    1 1
    1 1
    4 1
    3 3
    3 2
    1 2
    2 3
    1 1
    1 1
    4 2
    1 1
    1 2
    1 1
    end
    label values concerned_chemicals concerned
    label values concerned_bacteria concerned
    label def concerned 1 "Not at all concerned", modify
    label def concerned 2 "Somewhat concerned", modify
    label def concerned 3 "Moderately concerned", modify
    label def concerned 4 "Very concerned", modify
    [/CODE]



  • #2
    Thanks for the data example.

    catplot is from SSC, as you're asked to explain (FAQ Advice #12). You're right; it doesn't offer to produce graphs like this.

    The ways I can think of for doing this are all slightly awkward, but it is certainly soluble. Here is one. I've abbreviated the value labels (not variable labels) for readability.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(concerned_chemicals concerned_bacteria)
    2 2
    1 1
    4 2
    4 4
    1 1
    2 2
    1 1
    1 2
    1 3
    3 1
    1 1
    1 1
    4 2
    4 4
    2 1
    1 2
    3 1
    3 1
    1 1
    1 1
    4 1
    1 1
    2 3
    1 1
    1 1
    1 2
    1 1
    4 3
    4 4
    4 3
    3 2
    2 1
    3 3
    1 1
    2 1
    3 1
    1 1
    1 1
    3 1
    1 1
    1 1
    4 2
    1 1
    1 1
    1 1
    1 1
    1 1
    2 2
    1 1
    1 1
    1 2
    3 2
    3 3
    2 2
    1 1
    2 1
    1 1
    2 2
    3 1
    2 2
    2 2
    1 1
    2 1
    1 1
    2 2
    2 2
    2 2
    1 3
    2 1
    1 2
    1 1
    1 1
    1 1
    4 1
    2 2
    2 2
    1 1
    4 4
    2 2
    2 2
    1 1
    4 4
    1 3
    2 1
    3 1
    2 3
    4 1
    1 1
    1 1
    4 1
    3 3
    3 2
    1 2
    2 3
    1 1
    1 1
    4 2
    1 1
    1 2
    1 1
    end
    label values concerned_chemicals concerned
    label values concerned_bacteria concerned
    label def concerned 1 "Not at all concerned", modify
    label def concerned 2 "Somewhat concerned", modify
    label def concerned 3 "Moderately concerned", modify
    label def concerned 4 "Very concerned", modify
    
    
    foreach s in chemicals bacteria {
    bysort concerned_`s' : gen `s' = _N
    }
    gen offset_b = concerned_bacteria - 0.1
    gen offset_c = concerned_chemicals + 0.1
    
    set scheme s1color 
    
    twoway bar chemicals offset_c, barw(0.2) blcolor(red) bfcolor(red*0.2) ///
    || bar bacteria offset_b, barw(0.2) blcolor(blue) bfcolor(blue*0.2) xla(1 "Not at all" 2 "Somewhat" 3 "Moderately" 4 "Very", noticks) /// 
    xtitle(Level of concern) legend(pos(1) col(1) ring(0)) ytitle(Number of replies) yla(, ang(h)) xsc(titlegap(*5))
    Click image for larger version

Name:	concerned.png
Views:	1
Size:	21.2 KB
ID:	1599888

    Comment


    • #3
      Another answer starts with


      Code:
      gen id = _n
      reshape long concerned_ , i(id) j(which) string 
      
      graph bar (count), over(which) over(concerned, relabel(1 "Not at all" 2 "Somewhat" 3 "Moderately" 4 "Very")) asyvars

      Comment


      • #4
        Thank you! I would have never figured this one out on my own... It looks fantastic!

        Comment


        • #5
          Good, but don't be fooled. The similarity of marginal distributions doesn't imply similar answers. Only 60% of the sample shown give the same ratings.


          Click image for larger version

Name:	concerned2.png
Views:	1
Size:	30.5 KB
ID:	1599934

          Comment

          Working...
          X