4种不同的情节成为独特的情节

问题描述

我使用以下示例数据集:

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,ncol=2,nrow=1)
page2 = ggarrange(plot3,plot4,nrow=1)
plots = list(page1,page2)
pdf('test_plots.pdf')
plots
dev.off()

就像我在代码中所说的那样,当我分别打印图时,它是有效的,但是当我将它们组合成PDF时,我得到的图是相同图的4倍。
我不明白我的错误在哪里。

解决方法

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

XSLFTable.addRow()

输出将是这样,并保存在pdf 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') 中:

enter image description here

第二个选项是处理数据并根据您希望在每张幻灯片中绘制多少个图来创建关键点。这里的代码:

Myplot.pdf

最终的pdf幻灯片可以通过以下方式获得:

#Code option 2
#Process data
dfp <- df %>% pivot_longer(-gene) %>%
  #Separate sample type
  separate(name,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,color=gene))+
    geom_point()+
    facet_wrap(.~sample,scales = 'free')+
    theme_bw()
  return(G)
}
#Apply
List2 <- lapply(List,myplot)

它看起来像这样:

enter image description here

enter image description here

它将显示在两个幻灯片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',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]],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更改为所需的数字即可。