如何在 ggplot 中使用不同 colors/textures 可视化条形图中两个参数的交互?
How to visualise interaction of two parameters in a barplot with different colors/textures in 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" 相关的后两个条为白色。然后,我想通过更改条内的纹理(条纹?)或以其他方式(更粗的线条?)突出显示与 "control" 组相关的第一条和第三条。
关于如何单独调整栏的外观的任何提示?
编辑:这是一个非常丑陋的例子,是用我想象中的东西画的:
这应该有助于解决你问题的颜色部分(我删除了所有与问题无关的代码部分):
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 .
我想使用颜色或纹理可视化条形图中两个因素之间的相互作用。
我首先绘制了一个因素(分数)的两个水平之间的差异,现在我想根据第二个因素(组)拆分这些水平。我的原始图通过对条形图进行不同着色(黑色与白色)来显示分数类型 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" 相关的后两个条为白色。然后,我想通过更改条内的纹理(条纹?)或以其他方式(更粗的线条?)突出显示与 "control" 组相关的第一条和第三条。 关于如何单独调整栏的外观的任何提示?
编辑:这是一个非常丑陋的例子,是用我想象中的东西画的:
这应该有助于解决你问题的颜色部分(我删除了所有与问题无关的代码部分):
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 .