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)

结果展示:

欢迎大家交流指正!

相关推荐
深度学习lover40 分钟前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
XiaoLeisj2 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
API快乐传递者2 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
励志成为嵌入式工程师3 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
捕鲸叉3 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer3 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq3 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
阡之尘埃4 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
记录成长java5 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
前端青山5 小时前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js