以下是我的数据:
mydata=data.frame(compliance=c("Yes","Yes","No","Yes","Yes","No"),
doctor=c("Sam","Sam","Sam","Bob","Fred","Bob"))我尝试创建如下条形图:
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")) 但是,条形颜色仍然是灰色的,我想知道如何解决这个问题,谢谢!

发布于 2021-08-09 03:04:26
不要使用两个scale_.*。您可以在scale_fill_manual本身中添加labels。
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"))

https://stackoverflow.com/questions/68706365
复制相似问题