r - 在 R 中自定义条形图的颜色
问题描述
这是一些用于制作条形图的可重现代码。我想将条 C、M、S 和 S 的颜色自定义为不同深浅的蓝色,将 V 自定义为橙色。我怎样才能做到这一点?谢谢!
IDs <- seq(1,50)
IDs <- data.frame(rep(IDs, each = 5))
names(IDs)[1] <- "ID"
tastes <- c("Strawberry", "Vanilla", "Chocolate", "Matcha", "Sesame")
tastes <- data.frame(rep(tastes, times = 50))
#random numbers for schools
A <- runif(250, 1,5)
B <- runif(250, 1,5)
C <- runif(250, 1,5)
#merge
test <- cbind(IDs, tastes)
test <- cbind(test, A)
test <- cbind(test, B)
test <- cbind(test, C)
names(test)[2] <- "Flavour"
#make long
test_long <- melt(test,
id.vars = c("ID", "Flavour"))
#plot
plot <- ggplot(test_long) +
geom_bar(aes(x = Flavour,
y = value), stat="summary", fun=mean) +
scale_x_discrete(labels=c("C","M","S","S","V")) +
coord_cartesian(ylim=c(1,5)) +
facet_grid(. ~ variable) +
labs(title = "Likeability of Different Flavours by School") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
plot
解决方案
推荐阅读
- javascript - 在 Vue 组件中运行回调并在更改状态之前等待结果
- r - 是否可以使用 RMarkdown 从字符串变量生成 HTML 中的上标?
- python - 安装包 halo 会出现错误:“install_requires”必须是包含有效项目/版本要求说明符的字符串或字符串列表
- gatsby - Facebook 和 Twitter 可以预览时,Whatsapp 预览不起作用
- unix - UNIX shell 脚本读取 csv
- git - 使用 Jenkins 管道将文件夹从旧分支复制到 SCM 正在使用的分支中
- macos - 无法在 macOS 上将 AAC/AptX 编解码器用于蓝牙音频
- azure-devops - 在包更新时触发构建
- python - pyftpsync 在输出中添加 ESC 字符
- aws-cli - AWS QuickSight 新的 api 操作最近宣布在 CLI 1.16.200 中不可用