r - ggplot2:降低色调作为可视化因子变量的一种手段
问题描述
我正在尝试想象一些东西,目前看起来类似于:
library(ggplot2)
library(ggpubr)
head(ToothGrowth)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# what I got:
p <- ggpaired(ToothGrowth, x="supp", y="len", fill="dose", line.color = "gray", line.size = 0.4)+
scale_fill_manual(name="Dosis", labels=c("0.5","1","2"),
values = c("darkorange2","olivedrab","cadetblue4"))+ # <= these colours are fixed as used in all other graphs already
facet_grid(~dose)+
stat_compare_means(method = "t.test", paired = TRUE)
p
现在我想根据条件(变量'supp'),相同颜色的两个盒子在色调上有所不同;例如,与 VC 相比,OJ 总是显得褪色。我知道调色板等有很多选项,但不幸的是,配色方案是固定的,因为它在所有其他图中设置并与共同作者共享以进行连贯设计。
# what I tried so far:
p1 <- p +
scale_alpha(name="supp", labels=c("OJ","VC"), range = c(0.5,1))
p1 # doesn't change anything
p2 <- p +
scale_fill_hue(name="supp", labels=c("OJ","VC"), h.start = c(0.5))
p2 # overwrites existing scale_fill (and does not do a hue)
p3 <- ggpaired(ToothGrowth, x="supp", y="len", fill="dose", hue="supp",
line.color = "gray", line.size = 0.4)+
scale_fill_manual(name="Dosis", labels=c("0.5","1","2"),
values = c("darkorange2","olivedrab","cadetblue4"))+
facet_grid(~dose)+
stat_compare_means(method = "t.test", paired = TRUE)
p3 # 'hue =' doesn't change anything
p4 <- ggpaired(ToothGrowth, x="supp", y="len", fill="dose", alpha="supp",
line.color = "gray", line.size = 0.4)+
scale_fill_manual(name="Dosis", labels=c("0.5","1","2"),
values = c("darkorange2","olivedrab","cadetblue4"))+
facet_grid(~dose)+
stat_compare_means(method = "t.test", paired = TRUE)
p4 # 'alpha =' doesn't change anything
另外,通常我会在正常的“ggplot”命令中做所有事情,但是由于我需要这种成对比较,所以我认为我坚持使用“ggpaired”;还是我不是?
有人对这个有什么好主意吗?提前致谢!
解决方案
你可以在 ggplot 中原生地完成整个事情,它只需要更多的代码。你可以这样做:
ggplot(ToothGrowth, aes(supp, len, fill = dose, alpha = supp)) +
geom_boxplot() +
scale_fill_manual(name = "Dosis",
labels = c("0.5", "1", "2"),
values = c("darkorange2", "olivedrab", "cadetblue4")) +
scale_alpha_discrete(range = c(0.5, 1),
guide = guide_none()) +
geom_line(inherit.aes = FALSE,
aes(supp, len, group = 0:59 %% 30),
color = "gray75") +
geom_text(data = data.frame(
x = 1.5,
y = 40,
dose = c("0.5", "1", "2"),
pval = sapply(c("0.5", "1", "2"), function(x) {
round(t.test(len ~ supp,
data = ToothGrowth[ToothGrowth$dose == x,],
paired = TRUE)$p.val, 4)})),
inherit.aes = FALSE,
aes(x = 1.5, y = 40, label = paste("T test: p value =", pval)),
check_overlap = TRUE) +
facet_grid(~dose) +
theme_classic() +
theme(legend.position = "top",
strip.background = element_rect(fill = "gray95", size = 0.25))
推荐阅读
- form-submit - html联系表单提交没有响应
- c# - 无法连接到 SQL Server 会话数据库异常 - 所有池连接都在使用中
- django - 通过 Django 在 Cloudinary 中使用可变高度和宽度调整缩略图大小
- django - Django 单元测试:AssertEqual 不将 PermissionDenied 与 403 进行比较,并在 get_object() 中提前终止
- php - Laravel:安排工作或工匠命令?
- c# - 如何从另一个静态类订阅一个类中的事件?
- procmail - procmail - 使用 {} 不会传送到 mbox
- flutter - Flutter/provider——用构造函数初始化一个状态
- powershell - powershell string urldecode 执行时间
- python - 从 Python 数据框中的整数中检索工作日名称