1

I have two data frames that include information on the same variable. However, in one of them, the variable does not take on all possible values. Therefore, geom_bar() changes the color of the bars, if there are not all categories in the data frame. I tried setting drop=FALSE, but this did not help. Following is a reproducible R example that describes my problem:

data1 <- data.frame(x=sample(c(1:4), size=1000, replace=T, prob=c(0,0.25,0.25,0.5)))
positions <- c("1", "2", "3", "4")
ggplot(data1, aes(as.factor(x), fill=as.factor(x))) + geom_bar(aes(y = 100*(..count..)/sum(..count..))) + 
  scale_fill_discrete(drop=FALSE) + scale_x_discrete(limits = positions, drop=FALSE)

enter image description here

data2 <- data.frame(x=sample(c(1:4), size=1000, replace=T, prob=c(0.25,0.25,0.25,0.25)))
ggplot(data2, aes(as.factor(x), fill=as.factor(x))) + geom_bar(aes(y = 100*(..count..)/sum(..count..))) + 
  scale_fill_discrete(drop=FALSE) + scale_x_discrete(limits = positions, drop=FALSE)

enter image description here

How can I make the colors for each category be the same in both plots?

LuckyPal
  • 345
  • 2
  • 11
  • Maybe you will find [this](https://stackoverflow.com/questions/38788357/change-bar-plot-colour-in-geom-bar-with-ggplot2-in-r) useful :) – Ana Jan 09 '20 at 08:07
  • Thank you! :) Indeed, the linked answers were helpful. Adding `+ scale_fill_manual(values = c("1"="red", "2"="green", "3"="blue", "4"="orange"))` did the trick. – LuckyPal Jan 09 '20 at 08:16

0 Answers0