首页 > 解决方案 > 如何在 ggplot 中用不同颜色/纹理可视化条形图中两个参数的交互?

问题描述

我想使用颜色或纹理可视化条形图中两个因素之间的相互作用。

我首先绘制了一个因素(分数)的两个水平之间的差异,现在我想根据第二个因素(组)分割这些水平。我的原始图通过将条形着色不同(黑色与白色)来显示分数类型 1 和 2 之间的差异。在我的第二个情节中,我仍然想强调这种配色方案,但我不确定如何做到这一点。

这是一些示例代码:

## Example data, data from two groups: patients and controls
    data_ex <- data.frame( pnum = c(1,2,3,4,5,6,7,8,9,10),
               group = c("patient", "patient","patient","patient","patient",
"control", "control", "control", "control", "control"),
               score1 = c(26, 15, 17, 15, 20, 21, 18, 19, 16, 20),
               score2 = c(25, 19, 14, 18, 20, 22, 17, 19, 18, 19))

## Reshape the data
    data_ex_long <- data_ex %>% gather(key = type_score, value = score, score1, score2)

我的第一个情节是这样的:

 ggplot(data=data_ex_long,aes(x = type_score, y = score)) +  
          geom_bar( width=.3, stat = "summary",  colour="black", size=1.5, fill =c("#252525","#f0f0f0"), position = position_dodge(width = 0.05),lwd=0.2) +
          geom_point( size = 3.5, position=position_jitter(width=0.05), alpha = 0.8, fill="#bdbdbd", colour = "black", shape = 21) +
          stat_boxplot(geom = "errorbar", width = 0.0) +
          theme(text = element_text(size = 18),
                axis.text.x  = element_text(size=15, color="#000000"),     
                axis.text.y  = element_text(size=20, color="#000000"),       
                axis.title.x = element_blank(),  
                axis.title.y = element_text(size=18, color="#000000"),
                axis.line.x  = element_line(colour = "black", size = 1.3), 
                axis.line.y  = element_line(colour = "black", size = 1.3),
                panel.border = element_blank(),
                panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(),
                panel.background = element_blank(),
                axis.ticks.length = unit(.25, "cm"),
                axis.line = element_line())

条形图

现在我想根据组变量拆分因子,如下所示:

        ggplot(data=data_ex_long,aes(x = type_score, y = score, fill = group)) +  
          geom_bar(stat = "summary",fun.y = "mean", colour="black", size=1.5, position = position_dodge(width = 1)) +
          geom_point(aes(type_score, fill = group), colour="black", size = 3.5, shape = 21, position = 
          position_jitterdodge(jitter.width = 0.2, jitter.height=0.4, dodge.width=0.9), alpha = 0.8) +
          geom_errorbar(aes(size=2),stat = 'summary', position = 'dodge', color = "black", width = 1, size = 1) +
          theme(text = element_text(size = 18),
                axis.text.x  = element_text(size=15, color="#000000"),     
                axis.text.y  = element_text(size=20, color="#000000"),       
                axis.title.x = element_blank(),  
                axis.title.y = element_text(size=18, color="#000000"),
                axis.line.x  = element_line(colour = "black", size = 1.3), 
                axis.line.y  = element_line(colour = "black", size = 1.3),
                panel.border = element_blank(),
                panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                panel.grid.minor.x = element_blank(), panel.grid.major.x = element_blank(),
                panel.background = element_blank(),
                axis.ticks.length = unit(.25, "cm"),
                axis.line = element_line())

交互图

我对这种着色方式不满意。我希望与“score1”相关的前两个条为黑色(或至少为深灰色),与“score2”相关的后两个条为白色。然后,我想突出显示与“控制”组相关的第一个和第三个条,方法是通过更改条内的纹理(条纹?)或以其他方式(粗线?)。关于如何单独调整条形外观的任何提示?

编辑:这是一个非常丑陋的例子,用我想象的东西做的: 示例图

标签: rggplot2plotinteraction

解决方案


这应该有助于您问题的颜色部分(我删除了所有与问题无关的代码部分):

ggplot(data=data_ex_long,aes(x = type_score, y = score, fill = type_score, group = group )) +  
  geom_bar(aes(col = group),stat = "summary",fun.y = "mean", size=1.5, position = position_dodge(width = 1)) +
  geom_point(aes(type_score, fill = type_score), colour="black", size = 3.5, shape = 21, position = 
               position_jitterdodge(jitter.width = 0.2, jitter.height=0.4, dodge.width=0.9), alpha = 0.8)  +
  scale_fill_manual(values = c("grey", "white", "grey", "white")) + 
  scale_color_manual(values = c("darkgreen", "steelblue"))

在此处输入图像描述

关于纹理,我发现了一个名为patternplot(可在 cran 上获得)的包,它可能对此有所帮助。它产生ggplot2对象,但是,语法似乎很奇怪。您还可以查看 github 存储库 clauswilke/ggtextures 。


推荐阅读