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)

结果展示:

欢迎大家交流指正!

相关推荐
PieroPc21 分钟前
Python 写的 智慧记 进销存 辅助 程序 导入导出 excel 可打印
开发语言·python·excel
2401_857439693 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna3 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
梧桐树04294 小时前
python常用内建模块:collections
python
Dream_Snowar4 小时前
速通Python 第三节
开发语言·python
高山我梦口香糖5 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
信号处理学渣6 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客6 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
蓝天星空6 小时前
Python调用open ai接口
人工智能·python