更改R中绘图上条形的颜色

时间:2019-11-14 22:31:31

标签: r ggplot2

library(ggplot2)

df <- data.frame(trt=c("TAVERN","Long Term Care","Grocery Store","Restaurant"),
                 outcome=c("a","b","c","d"))

ggplot(df,aes(trt, outcome)) + 
  geom_col() +
  geom_point(colour = 'red') +
  ggtitle("Failing rate in different facility types") + 
  labs(x="Facility type",y="Failing rate") + 
  theme(panel.background = element_blank()) +
  scale_color_manual(values = c("purple","green", "red", "orange"))

我尝试手动更改ggplot中的颜色。

enter image description here

但是我没有这样做。真的很困惑。

问题。如何依次设置ggplot()函数的参数 改变条形的颜色?

1 个答案:

答案 0 :(得分:1)

我假设您想更改条形的颜色。然后,您需要指定fill的{​​{1}}选项。

geom_col

或者,您可以设置library(ggplot2) df <- data.frame(trt=c("TAVERN","Long Term Care","Grocery Store","Restaurant"), outcome=c("a","b","c","d")) ggplot(df,aes(trt, outcome)) + geom_col(fill=c("purple","green", "red", "orange")) + geom_point(colour = 'red') + ggtitle("Failing rate in different facility types") + labs(x="Facility type", y="Failing rate") + theme(panel.background = element_blank()) 美观。请注意,fill用于内部。 fill用于边界。

colour