美文网首页
ggplot2画分组蜜蜂图

ggplot2画分组蜜蜂图

作者: 旭日_太阳的后裔 | 来源:发表于2023-04-10 22:52 被阅读0次

    构建数据

    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画分组蜜蜂图

          本文链接:https://www.haomeiwen.com/subject/qigwddtx.html