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)
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)
How can I make the colors for each category be the same in both plots?