ggplot2画分组蜜蜂图

构建数据

data = data.frame(TN = sample(c("Tumor","Normal"),300,replace=T),
                  variable = sample(c("g1","g2","g3"),300,replace=T),
                  value = rnorm(300, mean = 50, sd = 10))

开始画图

library(ggplot2)
library(ggbeeswarm)
ggplot(data, aes(x = variable, y = value, color = TN)) +
  geom_beeswarm(#cex = 2, 
    #priority = "density", 
    shape = 23,
    cex = 1, 
    corral = "gutter", #捕获逃逸点方式
    corral.width = 0.3, #组内宽度
    dodge.width = 0.7) + #组间宽度
  #添加中位数,上、下四分位数指示线
  stat_summary(data = data,
                   position = "dodge",#dodge 类似柱状图中让分组并排
                   mapping = aes(x = variable, y = value,
                                 group=TN),  #关键参数 group####
                   fun.y = median, fun.ymin = median, fun.ymax = median,
                   geom = 'crossbar', 
                   width = 0.7, #控制横线宽度
                   size = 0.3,#控制横线粗线
                   color = 'black') +
  stat_summary(data = data,
                   position = "dodge",#dodge,identity
                   mapping = aes(x = variable, y = value,
                                 group=TN),#关键参数 group####
                   fun.data = function(x) median_hilow(x, 0.5),
                   geom = 'errorbar',
                   width = 0.7, #控制横线宽度
                   color = 'black')+
  scale_color_manual(values = c("blue", "red")) +
  scale_y_continuous(position = "left") +
  #coord_flip() + 
  #scale_x_discrete(limits = rev(level)) + 
  theme(axis.line = element_line(colour = "black"),
        panel.background = element_blank(),
        axis.ticks = element_line(),
        axis.text = element_text(color = "black")) + 
  labs(x = " " , y = "")

结果

微信图片_20230411225217.png

你可能感兴趣的:(ggplot2画分组蜜蜂图)