更改bar ggplot2的颜色

时间:2015-11-25 00:03:14

标签: r colors ggplot2

我正在尝试使用此代码更改条形图的颜色,但它似乎不起作用。有什么问题?

ggplot(hd.m, aes(provinces, value)) +  geom_bar(aes(fill="#0072B2"), position   = "dodge", stat="identity") + scale_fill_discrete(guide=FALSE) + xlab("Provinces and Territories") + ylab("Percentage(%)") + ggtitle("Heart Disease Prevelance across Canada in 2008-2009") + theme_bw() + theme(panel.border = element_blank()) + theme(
  plot.title = element_text(size=20),
  axis.title.x = element_text(size=14),      axis.title.y = element_text(size=14)) + geom_hline(yintercept=4.7)

enter image description here

1 个答案:

答案 0 :(得分:2)

填写aes。

library(ggplot2)
df <- data.frame(x = c("AB","BC","MB"), y = c(3.5,3.9,4.6))

# You have:
ggplot(df, aes(x,y)) + geom_bar(aes(fill="blue"), stat="identity")

# Try:
ggplot(df, aes(x,y)) + geom_bar(fill="blue", stat="identity")