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)

结果展示:

欢迎大家交流指正!

相关推荐
糖豆豆今天也要努力鸭几秒前
torch.__version__的torch版本和conda list的torch版本不一致
linux·pytorch·python·深度学习·conda·torch
何大春16 分钟前
【弱监督语义分割】Self-supervised Image-specific Prototype Exploration for WSSS 论文阅读
论文阅读·人工智能·python·深度学习·论文笔记·原型模式
在下不上天25 分钟前
Flume日志采集系统的部署,实现flume负载均衡,flume故障恢复
大数据·开发语言·python
SEVEN-YEARS28 分钟前
深入理解TensorFlow中的形状处理函数
人工智能·python·tensorflow
EterNity_TiMe_33 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
陌小呆^O^38 分钟前
Cmakelist.txt之win-c-udp-client
c语言·开发语言·udp
Suyuoa44 分钟前
附录2-pytorch yolov5目标检测
python·深度学习·yolo
布说在见1 小时前
个人实施工作的一天 —— 繁琐的数据输入与未来的句里录数据
经验分享·实习实施
I_Am_Me_1 小时前
【JavaEE进阶】 JavaScript
开发语言·javascript·ecmascript
重生之我是数学王子1 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt