云雨图——一段简单代码的分享

2022-07-20  本文已影响0人  Bio_Infor
分享一段绘制云雨图的代码,这个图由于其“比较高的颜值”,前段时间在各个生信交流群传的挺火……

数据来源

简单起见,测试数据来源为ggplot2内置的mpg数据集。

代码

library(ggplot2)
if(!require(PupillometryR)) install.packages('PupillometryR')

head(mpg)

ggplot(data = mpg, aes(x = class, y = hwy)) + 
  geom_flat_violin(aes(fill = class), 
                   position = position_nudge(x = .25), #调整小提琴图的位置,使其不要位于正中间,而向右偏移
                   color = 'black',
                   trim = F) + 
  geom_jitter(aes(color = class), width = .1) +
  geom_boxplot(width = .1, 
               position = position_nudge(x = .25), #调整箱线图的位置,使其不要位于正中间,而向右偏移
               fill = 'white', 
               size = .5) + 
  theme_test() +
  theme(legend.position = 'none') #隐藏图例,因为图例和横坐标信息的冗余的

引入了一个新包:PupillometryR和其对应的一个函数geom_flat_violin(),这段代码粘贴即可运行,这个图其实就是把小提琴图展示数据分布的特征用点去做了进一步的可视化

上一篇 下一篇

猜你喜欢

热点阅读