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

箱线图绘制

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

1. 写在前面

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

2.箱线图绘制

2.1 相关R包导入

library(openxlsx)
library(tidyverse)
library(ggsignif)
library(ggpubr)
library(RColorBrewer)
library(ggplot2)

2.2 数据导入及格式转换

  由于使用ggplot2进行绘图需要将原本的表格数据进行转换,一下为数据导入和转换方法:
数据格式:
R语言:箱线图绘制(添加平均值趋势线)_第1张图片

R语言:箱线图绘制(添加平均值趋势线)_第2张图片

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"))

数据转换格式:

> 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
> 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绘图

(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)
    )
)

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

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)

结果展示:

R语言:箱线图绘制(添加平均值趋势线)_第3张图片

欢迎大家交流指正!

你可能感兴趣的:(R语言,r语言,python,开发语言,经验分享,GIS,ggplot2)