最近处理扩增子数据,很多数据需要转化整理,从而适用于ggplot2系列的可视化工作,之前常常在Excel表格中手动整理,今天记录写一下使用tidyr包和dplyr包进行数据整理的过程,最后使用ggplot2进行批量作图,cowplot包对图进行组合。
1.载入包
library(tidyverse)
list.files()
2.长宽数据转换
family_data <- read_tsv('C:/Users/Administrator/Documents/R_work/03_BD_L_microbiome/00_rawdata/outfiles/expr.relative_abundance.abfam.txt')
head(family_data)
# A tibble: 6 x 19
Family `Bd-1-1` `Bd-1-2` `Bd-1-3` `Bd-1-4` `Bd-1-5` `Bd-1-6` `Bd-2-1` `Bd-2-2` `Bd-2-3`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Aceto~ 0.0224 0.0337 0.0398 5.63e-2 0.114 7.77e-2 0.00396 0.0648 2.23e-2
2 Acida~ 0.0109 0.00232 0.00648 4.22e-5 0 0. 0 0 1.61e-3
3 Actin~ 0.000613 0 0 0. 0 3.68e-5 0 0 0.
4 Akker~ 0 0 0 0. 0 0. 0.000967 0 0.
5 Archa~ 0 0 0 0. 0 0. 0.000193 0 8.79e-5
6 Bacil~ 0.0295 0.0549 0.0439 7.11e-2 0.0383 6.37e-2 0.0343 0.0153 1.64e-2
# ... with 9 more variables: `Bd-2-4` <dbl>, `Bd-2-5` <dbl>, `Bd-2-6` <dbl>, `Bd-3-1` <dbl>,
# `Bd-3-2` <dbl>, `Bd-3-3` <dbl>, `Bd-3-4` <dbl>, `Bd-3-5` <dbl>, `Bd-3-6` <dbl>
#宽数据转为长数据
family_data <- family_data %>%
pivot_longer(!Family, names_to = "Sample", values_to = "value")
head(family_data)
# A tibble: 1,278 x 3
Family Sample value
<chr> <chr> <dbl>
1 Acetobacteraceae Bd-1-1 0.0224
2 Acetobacteraceae Bd-1-2 0.0337
3 Acetobacteraceae Bd-1-3 0.0398
4 Acetobacteraceae Bd-1-4 0.0563
5 Acetobacteraceae Bd-1-5 0.114
6 Acetobacteraceae Bd-1-6 0.0777
7 Acetobacteraceae Bd-2-1 0.00396
8 Acetobacteraceae Bd-2-2 0.0648
9 Acetobacteraceae Bd-2-3 0.0223
10 Acetobacteraceae Bd-2-4 0.00813
# ... with 1,268 more rows
3.根据Sample列生成新列
family_data <- family_data %>%
mutate(Group = case_when(
startsWith(Sample, "Bd-1") ~ "First ",
startsWith(Sample, "Bd-2") ~ "Second ",
startsWith(Sample, "Bd-3") ~ "Third "),
num = case_when(
startsWith(Sample, "Bd-1") ~ 1,
startsWith(Sample, "Bd-2") ~ 2,
startsWith(Sample, "Bd-3") ~ 3))
head(family_data)
# A tibble: 1,278 x 5
Family Sample value Group num
<chr> <chr> <dbl> <chr> <dbl>
1 Acetobacteraceae Bd-1-1 0.0224 First 1
2 Acetobacteraceae Bd-1-2 0.0337 First 1
3 Acetobacteraceae Bd-1-3 0.0398 First 1
4 Acetobacteraceae Bd-1-4 0.0563 First 1
5 Acetobacteraceae Bd-1-5 0.114 First 1
6 Acetobacteraceae Bd-1-6 0.0777 First 1
7 Acetobacteraceae Bd-2-1 0.00396 Second 2
8 Acetobacteraceae Bd-2-2 0.0648 Second 2
9 Acetobacteraceae Bd-2-3 0.0223 Second 2
10 Acetobacteraceae Bd-2-4 0.00813 Second 2
# ... with 1,268 more rows
4.批量作图
#写一个作图函数
myboxplot.v1 <- function(df, taxo) {
p <- ggplot() +
geom_boxplot(data = filter(df, Family == taxo),
aes(x = Group, y = value*100, fill = Group)) +
geom_smooth(data = filter(df, Family == taxo),
aes(x = num, y = value*100),
size = 1.2, color = "grey40",level = 0.8, alpha = 0.3) +
labs(y = "Relative abundance (%)", x = "") +
ggtitle(taxo) +
theme(plot.title = element_text(size = 14, hjust = 0.5, face = "bold"),
strip.text = element_text(size = rel(3)),
axis.title.x = element_text(size = 14, vjust = 0.5,
hjust = 0.5),
axis.title.y = element_text(size = 13, vjust = 0.5,
hjust = 0.5,face = "bold",color ="black"),
axis.text.x = element_text(angle = 0, size = 9,
vjust = 0.5, hjust = 0.5,
face = "bold",color = "black"),
axis.text.y = element_text(size = 12,vjust = 0.5,
hjust = 0.5),
axis.line = element_line(colour = "black"),
panel.background = element_rect(fill = NA)) +
scale_fill_manual(values = c("#2874C5", "#EABF00", "#E64B35B2")) +
scale_x_discrete(labels = c('1st','2nd','3rd')) +
guides(fill = FALSE)
}
#读入需要作图的科水平菌群名
#将相对丰度大于1%的科水平菌群分为下降趋势类群和上升趋势类群,分别作图,并添加拟合曲线
down <- read.table("C:/Users/Administrator/Documents/R_work/03_BD_L_microbiome/00_rawdata/outfiles/02_down_family_ID.txt",as.is = T,row.names = 1 )
row.names(down)
[1] "Streptococcaceae" "Rhizobiaceae" "Acetobacteraceae" "Bacillaceae"
[5] "Burkholderiaceae" "Pseudomonadaceae"
up <- read.table("C:/Users/Administrator/Documents/R_work/03_BD_L_microbiome/00_rawdata/outfiles/03_up_family_ID.txt", as.is = T, row.names = 1)
row.names(up)
[1] "Enterobacteriaceae" "Holosporaceae" "Leuconostocaceae" "Lactobacillaceae"
#批量作图,并拼图
list_box1 <- lapply(row.names(up), myboxplot.v1, df = family_data)
mult_plot1 <- plot_grid(plotlist=list_box1, ncol= 2, nrow = 2)
list_box2 <- lapply(row.names(down), myboxplot.v1, df = family_data)
mult_plot2 <- plot_grid(plotlist=list_box2, ncol= 3, nrow = 2)
这个学习笔记记录了tidyr包pivot_longer函数将宽数据转化为长数据,dplyr包mutate函数生成新的数据列,最后学习使用function函数将ggplot2画图代码封装成一个函数,配合lapply函数对数据进行批量作图,最后使用cowplot包的plot_grid函数进行多图组合。