R语言:箱线图绘制(添加平均值趋势线)

箱线图绘制

  • [1. 写在前面](#1. 写在前面)
  • 2.箱线图绘制
    • [2.1 相关R包导入](#2.1 相关R包导入)
    • [2.2 数据导入及格式转换](#2.2 数据导入及格式转换)
    • [2.3 ggplot绘图](#2.3 ggplot绘图)

1. 写在前面

今天有时间把之前使用过的一些代码和大家分享,其中箱线图绘制我认为是非常有用的一个部分。之前我是比较喜欢使用origin进行绘图,但是绘制的图不太好看,并且需要进行不断调整,不太方便,所以开始使用R语言进行绘制。

2.箱线图绘制

2.1 相关R包导入

r 复制代码
library(openxlsx)
library(tidyverse)
library(ggsignif)
library(ggpubr)
library(RColorBrewer)
library(ggplot2)

2.2 数据导入及格式转换

由于使用ggplot2进行绘图需要将原本的表格数据进行转换,一下为数据导入和转换方法:

数据格式:

r 复制代码
setwd("C:/Users/Desktop/Practice/")
dataT1 <- read.xlsx("T1.xlsx", sheet = 1) # 文件名+sheet的序号
dataS1 <- read.xlsx("S1.xlsx", sheet = 1) # 文件名+sheet的序号
summary(dataT1)
head(dataT1)
summary(dataS1)
head(dataS1)


#使用tidyverse包对数据进行处理
dataT1 <- dataT1 %>% 
  gather(key = 'group',value = 'values') %>%  #gather()函数可以把多列数据合并成一列数据
  filter(!is.na(values)) 
head(dataT1) 
summary(dataT1)

dataS1 <- dataS1 %>% 
  gather(key = 'group',value = 'values') %>%  #gather()函数可以把多列数据合并成一列数据
  filter(!is.na(values)) 
head(dataS1) 
summary(dataS1)

dataT1$group<-factor(dataT1$group,levels = c("2dm","4dm","6dm","8dm","10dm"))
dataS1$group<-factor(dataS1$group,levels = c("2dm","4dm","6dm","8dm","10dm"))

数据转换格式:

r 复制代码
> head(dataT1)
  group values
1   2dm 0.8640
2   2dm 0.8500
3   2dm 0.8680
4   2dm 0.8850
5   2dm 0.8870
6   2dm 0.8951
r 复制代码
> head(dataS1)
  group values
1   2dm  0.619
2   2dm  0.610
3   2dm  0.632
4   2dm  0.700
5   2dm  0.679
6   2dm  0.711

2.3 ggplot绘图

r 复制代码
(p1 <- ggplot(data = dataT1, aes(x = group, y = values)) +
    stat_boxplot(geom = "errorbar", width = 0.3, size = 0.8, aes(color = group)) +
    geom_boxplot(aes(x = group, y = values, colour = group), size = 1.0, width = 0.6) +
    geom_jitter(mapping = aes(x = group, y = values, colour = group), size = 1.5, alpha = 0.3) +
    stat_summary(fun = "mean", geom = "point", color = "black", size = 2) +
    stat_summary(fun = "mean", geom = "line", aes(group = 1), color = "black", size = 1) +
    scale_color_manual(limits = c("2dm","4dm","6dm","8dm","10dm"), 
                       values = c("#8dd3c7", "#fdb462", "#bebada", "#fb8072", "#80b1d3")) +
    theme_classic(base_line_size = 1) +
    labs(x = "空间分割等级", y = "Kappa") +
    theme(
      text = element_text(size = 16, family = "serif"),
      axis.ticks.length = unit(0.2, "cm"),  # 设置刻度线的长度
      axis.ticks = element_line(size = 1),  # 设置刻度线的粗细
      legend.position = "none",
      plot.title = element_text(size = 16, colour = "black", hjust = 0.5),
      axis.title.y = element_text(size = 16, color = "black", vjust = 1.9, hjust = 0.5, angle = 90),
      legend.title = element_text(color = "black", size = 16),
      legend.text = element_text(color = "black", size = 16),
      axis.text.x = element_text(size = 16, color = "black", vjust = 0.5, hjust = 0.5, angle = 0),
      axis.text.y = element_text(size = 16, color = "black", vjust = 0.5, hjust = 0.5, angle = 0),
      panel.border = element_rect(color = "black", size = 1, fill = NA),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.line = element_line(colour = "black", size = 2),
      axis.line.x = element_line(colour = "black", size = 0),
      axis.line.y = element_line(colour = "black", size = 0)
    )
)


(p2 <- ggplot(data = dataS1, aes(x = group, y = values)) +
    stat_boxplot(geom = "errorbar", width = 0.3, size = 0.8, aes(color = group)) +
    geom_boxplot(aes(x = group, y = values, colour = group), size = 1.0, width = 0.6) +
    geom_jitter(mapping = aes(x = group, y = values, colour = group), size = 1.5, alpha = 0.3) +
    stat_summary(fun = "mean", geom = "point", color = "black", size = 2) +
    stat_summary(fun = "mean", geom = "line", aes(group = 1), color = "black", size = 1) +
    scale_color_manual(limits = c("2dm","4dm","6dm","8dm","10dm"), 
                       values = c("#8dd3c7", "#fdb462", "#bebada", "#fb8072", "#80b1d3")) +
    theme_classic(base_line_size = 1) +
    labs(x = "空间分割等级", y = "Kappa") +
    theme(
      text = element_text(size = 16, family = "serif"),
      axis.ticks.length = unit(0.2, "cm"),  # 设置刻度线的长度
      axis.ticks = element_line(size = 1),  # 设置刻度线的粗细
      legend.position = "none",
      plot.title = element_text(size = 16, colour = "black", hjust = 0.5),
      axis.title.y = element_text(size = 16, color = "black", vjust = 1.9, hjust = 0.5, angle = 90),
      legend.title = element_text(color = "black", size = 16),
      legend.text = element_text(color = "black", size = 16),
      axis.text.x = element_text(size = 16, color = "black", vjust = 0.5, hjust = 0.5, angle = 0),
      axis.text.y = element_text(size = 16, color = "black", vjust = 0.5, hjust = 0.5, angle = 0),
      panel.border = element_rect(color = "black", size = 1, fill = NA),
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.line = element_line(colour = "black", size = 2),
      axis.line.x = element_line(colour = "black", size = 0),
      axis.line.y = element_line(colour = "black", size = 0)
    )
)

最后将两张图进行拼接并保存:

r 复制代码
p1_cowplot <- ggdraw(p1)
p2_cowplot <- ggdraw(p2)
combined_plot <- plot_grid(p1_cowplot, p2_cowplot, ncol = 2, labels = "AUTO") #, labels = "AUTO"
combined_plot
ggsave("combined_plot.jpg", width = 28, height = 10, units = "cm", dpi= 600)

结果展示:

欢迎大家交流指正!

相关推荐
小爬菜6 分钟前
Django学习笔记(项目默认文件)-02
前端·数据库·笔记·python·学习·django
장숙혜10 分钟前
JavaScript正则表达式解析:模式、方法与实战案例
开发语言·javascript·正则表达式
安大小万27 分钟前
C++ 学习:深入理解 Linux 系统中的冯诺依曼架构
linux·开发语言·c++
随心Coding31 分钟前
【零基础入门Go语言】错误处理:如何更优雅地处理程序异常和错误
开发语言·后端·golang
T.Ree.35 分钟前
C语言_自定义类型(结构体,枚举,联合)
c语言·开发语言
Channing Lewis36 分钟前
python生成随机字符串
服务器·开发语言·python
资深设备全生命周期管理1 小时前
以Python 做服务器,N Robot 做客户端,小小UI,拿捏
服务器·python·ui
洪小帅1 小时前
Django 的 `Meta` 类和外键的使用
数据库·python·django·sqlite
小熊科研路(同名GZH)1 小时前
【Matlab高端绘图SCI绘图模板】第002期 绘制面积图
开发语言·matlab