美文网首页
R可视化——ggplot2绘制柱状图

R可视化——ggplot2绘制柱状图

作者: 科研那点事儿 | 来源:发表于2022-07-18 06:31 被阅读0次

    今天,小编带大家使用ggplot2包绘制一种比较基础的图形——柱状图!

    1、加载包

    #设置工作环境
    rm(list=ls())
    setwd("D:\\桌面\\柱状图")
    
    #加载包
    library(ggplot2)
    library(reshape2)
    library(ggpubr)
    library(ggprism)
    

    2、加载并处理数据

    #加载数据
    df <- read.table("data.txt",header = T, check.names = F)
    #转换数据
    data=melt(df)
    data$G<-rep(c("T","F","H"), each = 24)
    head(data)
    
    image.png
    image.png

    3、绘图

    1)绘制基础图形

    ggplot(data,aes(G,value,color=G,fill=G))+
      geom_bar(stat="summary",fun=mean,position="dodge")+
      stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.3)+
      theme_bw()
    
    image.png

    2)美化

    p1<-ggplot(data,aes(G,value,color=G,fill=G))+
      geom_bar(stat="summary",fun=mean,position="dodge")+ #绘制柱状图
      stat_summary(geom = "errorbar",fun.data = 'mean_sd', width = 0.3)+#误差棒
      labs(x="Samples",y=NULL)+#标题
      theme_prism(palette = "candy_bright",
                  base_fontface = "plain", # 字体样式,可选 bold, plain, italic
                  base_family = "serif", # 字体格式,可选 serif, sans, mono, Arial等
                  base_size = 16,  # 图形的字体大小
                  base_line_size = 0.8, # 坐标轴的粗细
                  axis_text_angle = 45)+ # 可选值有 0,45,90,270
      scale_fill_prism(palette = "candy_bright")#使用ggprism包修改颜色
    p1
    
    image.png

    4、拓展

    1)根据分组进行分面

    p2<-p1+facet_grid(~group,scales = 'free')
    p2
    
    image.png

    2)添加显著性

    p3<-p2+geom_signif(comparisons = list(c("F","H"),
                                          c("H","T"),
                                          c("F","T")),# 设置需要比较的组
                       map_signif_level = T, #是否使用星号显示
                       test = "t.test", ##计算方法
                       y_position = c(50,55,60),#图中横线位置设置
                       tip_length = c(c(0.01,0.01),
                                      c(0.01,0.01),
                                      c(0.01,0.01)),#横线下方的竖线设置
                       size=0.8,color="black")
    p3
    
    image.png

    3)添加散点或者抖动点

    p4<-p3+geom_point(data=data,aes(G,value),size=2,pch=20,color="black") 
    p4
    
    image.png
    p5<-p3+geom_jitter(width = 0.2,size=2,pch=20,color="black")
    p5
    
    image.png

    5、完整代码

    #设置工作环境
    rm(list=ls())
    setwd("D:\\桌面\\柱状图")
    
    #加载包
    library(ggplot2)
    library(reshape2)
    library(ggpubr)
    library(ggprism)
    
    #加载数据
    df <- read.table("data.txt",header = T, check.names = F)
    #转换数据
    data=melt(df)
    data$G<-rep(c("T","F","H"), each = 24)
    #绘图
    ggplot(data,aes(G,value,color=G,fill=G))+
      geom_bar(stat="summary",fun=mean,position="dodge")+ #绘制柱状图
      # geom_point(data=data,aes(G,value),size=2,pch=20,color="black")+#散点图
      geom_jitter(width = 0.2,size=2,pch=20,color="black")+#添加抖动点
      stat_summary(geom = "errorbar", fun.data = 'mean_sd', width = 0.3)+#误差棒
      labs(x="Samples",y=NULL)+#标题
      facet_grid(~group,scales = 'free')+#分面
      theme_prism(palette = "candy_bright",
                  base_fontface = "plain", # 字体样式,可选 bold, plain, italic
                  base_family = "serif", # 字体格式,可选 serif, sans, mono, Arial等
                  base_size = 16,  # 图形的字体大小
                  base_line_size = 0.8, # 坐标轴的粗细
                  axis_text_angle = 45)+ # 可选值有 0,45,90,270
      scale_fill_prism(palette = "candy_bright")+#使用ggprism包修改颜色
      geom_signif(comparisons = list(c("F","H"),
                                     c("H","T"),
                                     c("F","T")),# 设置需要比较的组
                  map_signif_level = T, #是否使用星号显示
                  test = "t.test", ##计算方法
                  y_position = c(50,55,60),#图中横线位置设置
                  tip_length = c(c(0.01,0.01),
                                 c(0.01,0.01),
                                 c(0.01,0.01)),#横线下方的竖线设置
                  size=0.8,color="black")
    

    相关文章

      网友评论

          本文标题:R可视化——ggplot2绘制柱状图

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