简体   繁体   English

结合数据中的图表并创建图表列表 R

[英]Combining graphs from data and creating a list of plots R

I think this may be a easy fix but I cannot get it to work.我认为这可能是一个简单的解决方法,但我无法让它工作。 This is for a R-shiny app down the line which has two input data sets structured like this:这是一个 R-shiny 应用程序,它有两个结构如下的输入数据集:

d1 <- data.frame(A1 = rnorm(10),
             A2 = rnorm(10),
             A3 = rnorm(10),
             B1 = rnorm(10),
             B2 = rnorm(10),
             B3 = rnorm(10),
             C1 = rnorm(10),
             C2 = rnorm(10),
             C3 = rnorm(10),
             Names = c("A", "B", "C", "D", "E",
                       "F", "G", "H", "I", "J"))

While this data is structured in a logical way representing replicates of the same sample this is not guaranteed in all cases and inputs vary from user to user.虽然此数据以逻辑方式构建,表示相同样本的重复,但这并不能保证在所有情况下,输入因用户而异。 Because of this I prompt for an index to indicate which columns are related (ie they get the same name) which gives a data frame that looks like this:因此,我提示输入一个索引来指示哪些列是相关的(即它们具有相同的名称),这给出了一个如下所示的数据框:

d2 <- data.frame(ID = c("A1", "A2", "A3", 
                    "B1", "B2", "B3",
                    "C1", "C2", "C3"),
             Anno = c(rep("A", 3), 
                      rep("B", 3),
                      rep("C", 3)), stringsAsFactors = FALSE)

I can compare each sample to every other sample with a scatterplot using this method我可以使用这种方法用散点图将每个样本与其他样本进行比较

plot_combinations = combn(names(d1)[1:ncol(d1)-1], 
                      2, 
                      simplify = FALSE)
plot_list <- list()
plot.col = "black"
for (i in 1:length(plot_combinations)) {
  p = ggplot(d1, 
             aes_string(x = plot_combinations[[i]][1], 
                        y = plot_combinations[[i]][2])) +
    geom_point(pch = 21, colour = "black", fill = plot.col) +
    theme_classic(base_size = 14)
  plot_list[[i]] = p
}

While in principle this works it is not feasible as with a small data set this is already 36 combinations.虽然原则上这是可行的,但它是不可行的,因为对于一个小数据集,这已经是 36 种组合。 What makes more sense is to compare within replicates instead.更有意义的是在重复中进行比较。 In order to do that I can rename the columns of d1 and get the frequency of each replicate and save it in a variable sampleFreq and extract data by looping over the replicate names like this:为了做到这一点,我可以重命名d1的列并获取每个复制的频率并将其保存在变量sampleFreq中,并通过循环遍历复制名称来提取数据,如下所示:

anno <- d2$Anno
d3 <- d1[,1:nrow(d1)-1]
colnames(d3) <- anno
sampleFreq <- as.data.frame(table(d2[,2]))
datalist <- list()
for (i in sampleFreq[, 1]) {
  d4 <- d3[, i == names(d3)]
  datalist[[i]] = d4
}

This gives me a list of data frames structured by replicate and I would like to create a list of scatter plots from this.这给了我一个由复制结构的数据框列表,我想从中创建一个散点图列表。 I have tried for loops but run into issues, I think there may be a lapply solution to this but I am stuck.我尝试过 for 循环,但遇到了问题,我认为可能有一个 lapply 解决方案,但我被卡住了。 The idea is to then call the plots from the plot list using a index number determined by number of times a button is clicked in shiny.这个想法是然后使用由在 shiny 中单击按钮的次数确定的索引号从 plot 列表中调用图。

Any help would be awesome任何帮助都是极好的

Thanks!谢谢!

What not do a pair plot within each replicate group?每个复制组中的一对 plot 不能做什么? We can start with your data, I try not to rename the columns in d1 to d3(like you did)我们可以从您的数据开始,我尽量不要将 d1 中的列重命名为 d3(就像您所做的那样)

d1 <- data.frame(A1 = rnorm(10),
             A2 = rnorm(10),
             A3 = rnorm(10),
             B1 = rnorm(10),
             B2 = rnorm(10),
             B3 = rnorm(10),
             C1 = rnorm(10),
             C2 = rnorm(10),
             C3 = rnorm(10),
             Names = c("A", "B", "C", "D", "E",
                       "F", "G", "H", "I", "J"))

d2 <- data.frame(ID = c("A1", "A2", "A3", 
                    "B1", "B2", "B3",
                    "C1", "C2", "C3"),
             Anno = c(rep("A", 3), 
                      rep("B", 3),
                      rep("C", 3)), stringsAsFactors = FALSE)

We go through all replicates:我们 go 通过所有重复:

pdf(<somefilename.pdf>)
for(i in unique(d2$Anno)){
COLS=d2$ID[d2$Anno ==i]
pairs(d1[,COLS],upper.panel=NULL)
}
dev.off()

Looks like this but it's (bloody) fast看起来像这样,但它(血腥)快

在此处输入图像描述

Or you can use GGally,或者你可以使用GGally,

library(GGally)

plots <- lapply(unique(d2$Anno),function(i){

    COLS=as.character(d2$ID[d2$Anno ==i])
    return(ggscatmat(d1[,d2$Anno %in% i]))
    })

plots[[1]]

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM