复现Nature communications期刊图,换数据即可用!
本次使用R语言复现一下Nature communications中的1张图,以及它的12种玩法!
✅读入测试数据
✅玩法1: boxplot + jitter point + half violin
原图由boxplot、jitter point 、half violin以及统计p值4部分构成:
即展示了原始数据的点(jitter point ); 又展示了数据的统计分布(boxplot); 3类图组合又节约版面空间(boxplot +jitter point + half violin);
一举3得!
# 核心代码
ggplot(iris_data, aes(x = species, y = sepal_length, fill = species)) +
# 添加右侧半边小提琴图
geom_half_violin(aes(fill = species)) +
# 左侧boxplot
geom_boxplot(aes(x = species, y = sepal_length, fill = 'black'))+
# 左侧jitter point
geom_point(aes(x = species, y = sepal_length, fill = species),position = position_jitter(width = .15))+
# 统计p值
geom_signif(
comparisons = list(
c("setosa", "versicolor"),
c("versicolor", "virginica"),
c("setosa", "virginica")
),
map_signif_level = TRUE, # 显示星号标记而非具体p值
y_position = c(8.5, 9.0, 9.5) # 设置标记的垂直位置,避免重叠
)
当然,这张图可以有更多玩法!
✅玩法2: boxplot + jitter point + half violin(水平)
只需使用coord_flip()即可,
# 核心代码
coord_flip()
✅玩法3: boxplot + jitter point + half violin(分开显示)
将boxplot、jitter point、half violin三图不组合在一起,通过添加position参数实现,
✅玩法4: boxplot + jitter point + half violin(分开显示 + 水平)
玩法3的基础上使用coord_flip()即可,
✅玩法5: boxplot + jitter point(分开显示)
玩法4的基础上去half violin即可,同时调整position,
✅玩法6: boxplot + jitter point(分开显示 + 水平)
玩法5的基础上使用coord_flip()即可,
✅玩法7: boxplot + jitter point(组合显示)
玩法5的基础上调整position,
✅玩法8: boxplot + jitter point(组合显示 + 水平)
玩法7的基础上使用coord_flip()即可,
✅玩法9: half boxplot + half jitter point(分开显示)
全部使用geom_half_xx代替geom_xx即可,
✅玩法10: half boxplot + half jitter point(分开显示 + 水平)
玩法9的基础上使用coord_flip()即可,
✅玩法11: half violin + half jitter point(分开显示)
玩法9的基础上替换violin方法即可,
✅玩法12: half violin + half jitter point(分开显示 + 水平)
上一玩法基础上添加coord_flip()即可,
本期结束!
(加入学习,备注:299)
(加入学习,收费,备注:299)