Announcement

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

  • Bar graph using categorical variables

    Hello! I would like to make a bar figure with 10 bars. I have a categorical variable with 5 options and baseline and endline data from each respondent. I would like each set of base and end bars to be grouped together for each of the 5 options, so for example, for the response "strongly disagree" one would see red and blue bars next to each other and a legend base = blue, end = green. My original dataset was one row per repondent ID and var_base and var_end values. I reshaped it to long for easier graphing. So now my dataset has var_ with a time variable indicating base and end. I have experimented with catplot and twoway with no success. With catpolot I was able to group 5 bars for baseline and 5 bars for endline but as I mentioned I want them grouped by response category. I am attaching a picutre of the kind of figure I would like to make (that I made in Excel). Thank you!
    Attached Files

  • #2
    Here is a way using graph bar. For your future posts, please familiarize yourself with the dataex command for presenting data examples.

    Code:
    help dataex
    Code:
    clear
    input float(which category val)
    1 1 1
    2 1 1
    1 2 6
    2 2 3
    1 3 17
    2 3 10
    1 4 52
    2 4 45
    1 5 23
    2 5 41
    end
    lab values which which
    lab values category cat
    lab def which 1 "Base" 2 "End", modify
    lab def cat 1 `""Strongly" "Disagree""' 2 "Disagree" 3 `""Neither" "disagree" "nor agree""' 4 "Agree" 5 "Strongly agree", modify
    
    gr bar val, over(which) over(cat) asyvars blab(total) ytitle("Percent") ylab("")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	35.1 KB
ID:	1748954

    Comment


    • #3
      Reverting to the mention of catplot (from SSC), note the difference between

      Code:
      sysuse auto, clear 
      
      catplot foreign rep78, percent(rep78) 
      
      catplot rep78 foreign, percent(rep78)

      Comment

      Working...
      X