首页 > 解决方案 > 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

具有成对比较的 3 个多面网格中的箱线图

现在我想根据条件(变量'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”;还是我不是?

有人对这个有什么好主意吗?提前致谢!

标签: rggplot2

解决方案


你可以在 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))

在此处输入图像描述


推荐阅读