Robin
Robin

Reputation: 459

How to manually change the fill color of barplot when stat='count' in ggplot2

Here is my data:

mydata=data.frame(compliance=c("Yes","Yes","No","Yes","Yes","No"),
                  doctor=c("Sam","Sam","Sam","Bob","Fred","Bob"))

I tried to creat the barplot as below:

ggplot(data=mydata, aes(x=doctor,fill=compliance)) +
  geom_bar(stat="count",width=0.4) +
  geom_text(stat='count',aes(label=..count..), vjust=-0.3) + 
  expand_limits(y = c(0, 3)) + 
  labs(y = NULL, x= NULL) +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values = c("#00BFC4","#F8766D")) + 
  scale_fill_discrete(limits = c("Not found","Compliance")) 

However, the bar color is still in grey, I'd like to know how to fix this problem, thanks! enter image description here

Upvotes: 2

Views: 1030

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389047

Don't use two scale_.*. You can add labels in scale_fill_manual itself.

library(ggplot2)

ggplot(data=mydata, aes(x=doctor,fill=compliance)) +
  geom_bar(stat="count",width=0.4) +
  geom_text(stat='count',aes(label=..count..), vjust=-0.3) + 
  expand_limits(y = c(0, 3)) + 
  labs(y = NULL, x= NULL) +
  theme(legend.title = element_blank()) +
  scale_fill_manual(values = c("#00BFC4","#F8766D"), labels = c("Not found","Compliance"))

enter image description here

Upvotes: 2

Related Questions