r - 如何用颜色填充箱线图?
问题描述
我想创建四个相邻的箱线图。它们都应该用另一种颜色填充。我试过: scale_fill_manual(values=c("1"="red", "2"="green", "3"="yellow", "4"="blue"))
options(stringsAsFactors = FALSE)
input <- "C:\\statistical_tests\\boxplot.csv"
boxplot<- read.csv(input, sep=";")
library(ggplot2)
library(scales)
means <- aggregate(number ~ CPOD, paper, mean)
p <- ggplot(boxplot, aes(group=time,y=number, x=as.character(time))) +
geom_boxplot()+ theme_bw() +
scale_fill_manual(values=c("1"="red", "2"="green", "3"="yellow", "4"="blue"))+
panel.grid.minor=element_line(colour="white"),
axis.text.x = element_text(size=8, colour="black"),
axis.text.y=element_text(size=8, colour="black"))
print(p)
不幸的是,它不起作用。有人可以帮助我吗?非常感谢!
解决方案
这可能是你想要的吗?
data("iris")
ggplot( iris, aes( x = Species, y = Sepal.Length, fill = Species ) ) +
geom_boxplot()
请注意,这仅在 x 轴变量是因子或字符时才有效。您可以在中定义它,data.frame
或者您可以使用更即时的方法,如下所示。当您不想更改原始数据的结构时,这很有用。
data("iris")
ggplot( iris, aes( x = as.factor(Species), y = Sepal.Length, fill = Species ) ) +
geom_boxplot()
推荐阅读
- sparql - Sparql-gremlin 工具不使用索引
- php - 单击按钮时如何遍历来自数据库的数据
- swift - 切换选项卡时大标题导航栏重叠
- windows - CMD 变量未在 for /r 循环中解析
- git - Go 模块用 v0.0.0 替换显式版本-
- 在 go.mod 中 - javascript - 获取mac中所有可见应用程序窗口进程的窗口标题和应用程序/进程名称
- php - php spredsheet 输出损坏的文件
- windows - 通过 openssl 创建根证书和服务器证书以在 Web 服务器上启用 https 在 Chrome 浏览器上显示 Not Secure - 所以 HTTPS 不起作用
- youtube - 我们可以在机器人框架中的视频卡上启用全屏选项吗?
- javascript - 网页登录后如何获取用户ID令牌