3
votes

I have the following bar graph and I would like to color the bar graphs (A, B, C) from category "1", to blue color. The bar graphs A,B,C from category "2" to green, "3" to yellow, "4" to brown, "5" to black...

I'm facing a really hard time trying to understand how can I do this, since when I tried to change the colors, it changes all of them to the same color...

my database is something like this:

ID var1 var2
sample1 A 1
sample2 B 1
sample3 C 1
sample4 C 1
sample5 A 2
sample6 B 3
sample7 C 2
sample8 A 4
sample9 A 4
sample10 A 5

To generate the graph below I've just used the command:

graph bar (count), over(var1) over(var2)

What I'm trying to get here is to color the groups 1, 2, 3, 4 and 5 in different colors, but the bars must be in the same color...

e.g. A,B,C from group 1 in blue, A,B,C from group 2 in green, A,B,C from group 3 in yellow, A,B,C from group 4 in brown and A,B,C from group 5 in black...

bar graph

2

2 Answers

3
votes

If you are willing to use a legend instead of labels, here is how I might do it:

sysuse auto, clear
label define lab 1 "A" 2 "B" 3 "C" 4 "D" 5 "E"
lab val rep78 lab
graph bar (mean) price, over(rep78) asyvars over(foreign) bar(1, color(blue)) bar(2, color(green))  bar(3, color(yellow)) bar(4, color(brown)) bar(5, color(black)) legend(rows(1))

This yields:

enter image description here

2
votes

To show a graph, this needs to be a question, not a comment.

This shows your categories 1 to 5 as different colours. See @Dimitriy Masterov's helpful answer for how to specify the colours you want.

clear 
set scheme s1color 
input ID str1 var1 var2
1 A 1
2 B 1
3 C 1
4 C 1
5 A 2
6 B 3
7 C 2
8 A 4
9 A 4
10 A 5
end 
graph bar (count), over(var2) over(var1) asyvars yla(0/2, ang(h)) legend(row(1))

enter image description here