在R中更改绘图条的颜色 库(ggplot2) df

在R中更改绘图条的颜色 库(ggplot2) df,r,ggplot2,R,Ggplot2,我假设您想要更改条的颜色。然后,您需要指定geom\u col的fill选项 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(colo

我假设您想要更改条的颜色。然后,您需要指定
geom\u col
fill
选项

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"))

您想更改条形或圆点的颜色吗?如果您想更改所需的条形图,您需要
缩放\u填充\u手动()
您还没有告诉ggplot给什么颜色或颜色应该来自哪里。在
aes
调用中,需要为颜色分配一些变量。另外,请记住,条形图有填充,因此您的色阶不会影响条形图的填充,就像@Mako212所说的那样。
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())
ggplot(df,aes(trt, outcome, fill=trt)) +
  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_fill_manual(values = c("purple","green", "red", "orange"))