r - 在 R 中更改绘图上的条形颜色
问题描述
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 中的颜色。
但我没有这样做。对此真的很困惑。
问题。如何设置ggplot()
函数参数以更改条形颜色?
解决方案
我假设您想更改条形的颜色。然后,您需要fill
指定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
是为边界。
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"))
推荐阅读
- javascript - 为了从 Dynamo 和 Lex 接收正确的数据,我应该在此代码中添加/删除什么?
- php - 如何列出相等行的数据库表数据?
- java - Mockito 无法模拟 Rest API 调用
- oracle - 根据子查询生效的 Oracle-Making 条件
- r - 使用带有 ggplot 的 geom_abline 进行绘图时,即使指定了坐标轴上也没有刻度线
- reactjs - 回调函数的返回值
- sql - 将 nvarchar 值转换为数据类型整数
- twilio - 关闭选项卡时从 Twilio 聊天频道中删除用户
- imagemagick - ImageMagick 旋转和裁剪批量图像
- django - nginx+gunicorn 解决静态内容的问题