1
votes

I want to use facet_wrap in R to split my plots based on a certain column. Here is a working example I reproduced from here:

set.seed(1)
df <- data.frame(age = runif(500, min = 10, max = 100), 
group = rep(c("a", "b", "c", "d", "e"), 100))

#Plotting
ggplot(df, aes(age)) + 
geom_histogram(aes(y = (..count..)), binwidth = 5) +
facet_wrap(~group, ncol = 3) 

This produces plots, all in grey color (shown below). However, I want each plot be in a specific color. That is, they have the following color in order c("green","orange","blue","black", "red"). All bars in plot (a) be green, all in (b) be orange, and so on. These colors match my other plots and preserve consistency. How can I achieve this task? Thanks.

enter image description here

1
Just add fill=group to your aes() call and use a scale_fill_manual().Limey
Thanks. But I need exactly specific color to be assigned to each plot. Not random!mah65
Fixed. Reread your question and posted an answer.Limey

1 Answers

2
votes
ggplot(df, aes(age)) + 
  geom_histogram(aes(y = (..count..), fill=group), binwidth = 5) +
  facet_wrap(~group, ncol = 3) +
  scale_fill_manual(values=c("green","orange","blue","black", "red"))

enter image description here