-1

My data frame is as follows

   School Gender Value ColorGroup
1 School1   Male    10      1Male
2 School1 Female    30    1Female
3 School2   Male    40      1Male
4 School2 Female    70    1Female
5 School3   Male     5      2Male
6 School3 Female    90    2Female

I can create the following bar charts

ggplot(data=data2, aes(x=School, y=Value, group = Gender, fill = Gender)) + 
  geom_bar(stat = "identity", position = position_dodge(), width =  0.5)

ggplot(data=data2, aes(x=School, y=Value, group = Gender, fill = ColorGroup)) + 
  geom_bar(stat = "identity", position = position_dodge(), width =  0.5)
  1. Bar chart 1 - Grouped by Gender enter image description here
  2. Barchart 2 - grouped by ColorGroup enter image description here

What I wish to do is specify the colors as follows School3 to have different colors from School1 and School2 using the following code

cols33 <- c("1Male" = "yellow", "1Female" = "orange", "2Male" = "red", "2Female" = "blue") 

ggplot(data=data2, aes(x=School, y=Value, group = Gender,fill = ColorGroup)) + 
  geom_bar(stat = "identity", position = position_dodge(), width =  0.5) + 
  scale_color_manual(name="",values=cols33)

but I still get the same output as barchart2. enter image description here

Please can you help understand why the scale_color_manual is not being honored in my code.

Vik G
  • 539
  • 3
  • 8
  • 22
  • 1
    `fill != color` – Henrik May 26 '18 at 11:23
  • Hi @Henrik - I could not follow your comment, using `fill != color` will not apply any colors to the bar charts. – Vik G May 26 '18 at 11:27
  • You have mapped **`fill`** color in `aes`. Thus, you need to use `scale_fill_manual`, not `scale_color_manual`. – Henrik May 26 '18 at 11:34
  • Thanks a ton @Henrik that helped solve the problem. I am going to make the change now. Following my example, would you know If there is an easy way to only show two out of the four legends, essentially only show legends for School3 from my example – Vik G May 26 '18 at 11:36
  • Please take your time to study the help texts, it's all in there. – Henrik May 26 '18 at 11:42
  • Cool, thanks for your help. – Vik G May 26 '18 at 11:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171826/discussion-between-vik-g-and-henrik). – Vik G May 26 '18 at 12:01
  • I might be missing something, but all three plots look identical...? – camille May 27 '18 at 00:56
  • Hi @camille - my question was answered by Henrik's suggestion. I had a follow up question [here](https://stackoverflow.com/questions/50548030/removing-two-legends-out-of-four-from-ggplot2) about removing some of the legends, if you have any ideas on how I can do that, it would be really helpful – Vik G May 27 '18 at 01:05
  • @VikG Clearly you didn't bother to read `?scale_fill_manual` as I suggested: "# As with other scales you can use `breaks` to control the appearance of the legend." – Henrik May 27 '18 at 09:58

1 Answers1

0

I had mapped fill color in aes. Thus, needed to use scale_fill_manual, not scale_color_manual.

Julian
  • 6,586
  • 2
  • 9
  • 33
Vik G
  • 539
  • 3
  • 8
  • 22