# Stacked bar charts of single variables

#### granen

##### New Member
Hi,

I am new to this forum but old in statistics. Nice to find such a good source of information.

I got a problem creating a stacked bar chart in stata that I need to make. It is for a survey report where the respondents have three possible choices, good, in between and bad. I need to present these on a single clustered bar charts. I have plenty of questions like this and need to present them in the same chart. I have attached an image of what I would like the result to look like. Does anyone now have to do this?

Best regards
Erik Granberg
Sweden
View attachment 1038

#### bukharin

##### RoboStataRaptor
You can create a dummy variable for each possible choice, then graph the mean of each one.

It's best to have your data in a long format, eg for 3 people answering 3 questions:
Code:
id question answer
1  1        2
1  2        3
1  3        3
2  1        1
2  2        1
2  3        2
3  1        1
3  2        3
3  3        2
(the id variable identifies each individual)

Now create 3 dummy variables representing the 3 answers:
Code:
gen a1=answer==1 if !missing(answer)
gen a3=answer==3 if !missing(answer)
Your dataset should now look like this:
Code:
id question answer a1 a2 a3
1  1        2      0  1  0
1  2        3      0  0  1
1  3        3      0  0  1
2  1        1      1  0  0
2  2        1      1  0  0
2  3        2      0  1  0
3  1        1      1  0  0
3  2        3      0  0  1
3  3        2      0  1  0
The mean of each dummy variable represents the proportion that gave that answer, eg for question 1, the mean of a2 is the proportion that answered 2 for question 1. Therefore the bars you want are the mean of a1, a2 and a3 for each question.

You can graph those means as follows:
Code:
graph hbar (mean) a1 a2 a3, over(question) stack
Obviously you may want to change the colours, improve the labels etc. You could also multiply each dummy variable by 100 to get % instead of proportion... But that's the gist of it. Attached is the result of the following commands, using the above data:

Code:
label define question 1 "Question 1" 2 "Question 2" 3 "Question 3"
label values question question
graph hbar (mean) a1 a2 a3, over(question) stack ///
legend(col(3) lab(1 "Answer 1") lab(2 "Answer 2") lab(3 "Answer 3"))

#### granen

##### New Member
Big thanks Bukharin, you really saved my day and helped me create really nice diagrams. I am writing a script to make the data in long format, for the future, is there any shortcuts for doing this?

//Erik

#### bukharin

##### RoboStataRaptor
You can use the -reshape- command to convert between wide and long format