4个不同的情节成为一个独特的情节

4 differents plots become a unique plot

我使用这个示例数据集:

gene smp1_A smp1_B smp2_A smp2_B smp3_A smp3_B smp4_A smp4_B
geneA 10 12 30 33 26 22 44 42
geneB 15 13 11 16 15 16 21 26

我想绘制 smp1_A vs smp1_Bsmp2_A vs smp2_B... = 4 个绘图
我想要一个 2 页的 PDF,在第一页 plot1plot2 以及第二页 plot3 等和 plot4.
(当然,我的真实数据集中还有很多图)。

library(ggplot2)
library(ggpubr)

data = read.table('test_data.txt',header=T)
samples = list('smp1','smp2','smp3','smp4')

for (i in 1:length(samples)){ 

    smp = samples[i]
    smpA = paste(smp,"A",sep="_")
    smpB = paste(smp,"B",sep="_")
        
    plot = ggplot(data, aes(x=data[,smpA], y=data[,smpB])) + geom_point()

    # I can't add the plot to a PDF in a loop, I have to generate it at the end
    # so I need to create a new variable each iteration to not overwrite the previous one 
    # I do it with assign

    nam <- paste("plot", i, sep = "")
    assign(nam, plot)
}

# at this point, if I try to plot my 4 plots separately, it's working fine.
# I have this 4 variables in my env : plot1, plot2, plot3, plot4 

# But now when I try to create my PDF I get 4 times the same plot and I can't figure out which one is it. 
page1 = ggarrange(plot1,plot2, ncol=2, nrow=1)
page2 = ggarrange(plot3,plot4, ncol=2, nrow=1)
plots = list(page1, page2)
pdf('test_plots.pdf')
plots
dev.off()

就像我在我的代码中所说的那样,当我单独打印我的图时它可以工作,但是当我将它们组合成一个 PDF 时我有 4 倍相同的图。
我不明白我的错误在哪里。

我建议两种方法。您可以以日志格式重塑数据并使用构面,或者您可以拆分重塑后的数据并使用函数按所需顺序创建绘图。这里是两个选项的代码。第一个选项是使用构面:

library(tidyverse)
#Code option 1
#Reshape data
df %>% pivot_longer(-gene) %>%
  #Separate sample type
  separate(name,into=c('sample','type'),sep = '_') %>%
  ggplot(aes(x=type,y=value,color=gene))+
  geom_point()+
  facet_wrap(.~sample,scales = 'free')+
  theme_bw()+
  ggsave(filename = 'Myplot.pdf',width = 35,height = 18,units = 'cm')

输出将是这样的并保存在 pdf 中 Myplot.pdf:

第二个选项是处理数据并根据每张幻灯片中所需的图表数量创建键。这里的代码:

#Code option 2
#Process data
dfp <- df %>% pivot_longer(-gene) %>%
  #Separate sample type
  separate(name,into=c('sample','type'),sep = '_')
#Keys
dfk <- data.frame(sample=unique(dfp$sample))
dfk$Key <- rep(1:2,each=2)
#Match
dfp <- dfp %>% left_join(dfk)
#Create list
List <- split(dfp,dfp$Key)
#Function for plot
myplot <- function(x)
{
  #Plot
  G <- ggplot(x,aes(x=type,y=value,color=gene))+
    geom_point()+
    facet_wrap(.~sample,scales = 'free')+
    theme_bw()
  return(G)
}
#Apply
List2 <- lapply(List,myplot)

最终pdf中的幻灯片可以这样获取:

#Export
pdf('Myexample.pdf',width = 14)
for(i in 1:length(List2))
{
  plot(List2[[i]])
}
dev.off()

它看起来像这样:

它将出现在两个幻灯片 pdf 中。

您可以尝试使用 lapply :

保留地块
data = data.frame(sapply(1:8,rnorm,n=10))
colnames(data) = paste(rep(c('smp1','smp2','smp3','smp4'),2),rep(c("A","B"),each=4),sep="_")

plts = lapply(list('smp1','smp2','smp3','smp4'),function(i){

 smpA = paste(i,"A",sep="_")
 smpB = paste(i,"B",sep="_")
        
 plt = ggplot(data, aes(x=!!ensym(smpA), y=!!ensym(smpB))) + 
 geom_point()
     
 return(plt)
})

names(plts) = paste0("plot",1:4)
 
page1 = ggarrange(plts[[1]],plts[[2]], ncol=2, nrow=1)
page2 = ggarrange(plts[[3]],plts[[4]], ncol=2, nrow=1)
pdf('test_plots.pdf')
print(page1);print(page2)
dev.off()

如果您以这种方式创建情节:

plot = ggplot(data, aes(x=data[,smpA], y=data[,smpB])) + 
  geom_point() + 
  ggtitle(paste(smpA, "vs", smpB))

你会发现每个情节都是不同的,即使它们看起来一样。


不过,我相信您的代码可以稍微调整一下。

我给你以下建议:

# your data
data <- read.table(text = "gene smp1_A smp1_B smp2_A smp2_B smp3_A smp3_B smp4_A smp4_B
geneA 10 12 30 33 26 22 44 42
geneB 15 13 11 16 15 16 21 26", header = TRUE)


# libraries
library(ggplot2)
library(patchwork)
library(dplyr)
library(tidyr)


# set up data
data <- data %>%
 pivot_longer(-gene) %>% 
 separate(name, into = c("smp", "letter")) %>% 
 pivot_wider(names_from = letter, values_from = value) 

# create plots 
df_plots <- data %>%
 nest_by(smp) %>%
 summarise(plot = list(ggplot(data) + geom_point(aes(x = A, y = B)) + ggtitle(smp)),
           .groups = "drop")
 
# create custom groups of plots
df_plots$n <- rep(seq_len(nrow(df_plots)), each = 2, length.out = nrow(df_plots))

# combine plots together
df_plots <- df_plots %>% 
 group_by(n) %>% 
 summarise(plot = list(Reduce(`+`, plot)), .groups = "drop") # possible thanks to patchwork

# print pdf
pdf('test_plots.pdf')
pull(df_plots, plot)
dev.off()

无论您有多少页或有多少图,解决方案都是灵活的。如果你想每页有 3 个或更多图,你只需要将 each = 2 更改为你想要的数字。