简体   繁体   English

使用 ggplot2 创建列表 plot

[英]create a list plot with ggplot2

I have 2 data frame that I want to plot with ggplot2 (geom_point) to create a list of plots我有 2 个数据框,我想用 plot 和 ggplot2 (geom_point) 创建一个绘图列表

ORDxyz ORDxyz

              NMDS1       NMDS2       NMDS3
CL3      -0.2137567  0.78090451 -1.12688987
CC1      -0.5831773  0.78430047 -0.83660547
SV1      -0.4015699  1.10454078 -0.89994576
M31Fcsw   2.2345702 -0.10825727  0.03557525
M11Fcsw   2.1697600 -0.22796133  0.33659027
M31Plmr   0.1607166  1.35753902  0.67096413
M11Plmr  -0.3634971  1.18194454  0.19043970
F21Plmr  -0.2021604  1.39640959  0.56176491
M31Tong   0.2369916 -0.14052293  1.01850442
M11Tong  -0.2848915  0.22303190  1.20694861
LMEpi24M -0.9342344  0.53757235  0.72241753
SLEpi20M -1.1537658  0.74220588  0.39799605
AQC1cm   -0.8072651  0.02708152 -0.26938989
AQC4cm   -1.0580555  0.03899654 -0.46868303
AQC7cm   -1.1022657  0.08630065 -0.55486503
NP2      -1.2830208 -0.94174259  0.72829504
NP3      -1.0019230 -1.14495602  0.50053261
NP5      -0.8401685 -1.22101902  0.80984706
TRRsed1  -0.7200720 -1.54233573 -0.45128179
TRRsed2  -0.8901108 -0.99928581 -0.75491396
TRRsed3  -0.4362564 -1.06123921 -0.81375694
TS28      2.0890177 -0.59166010 -0.15498612
TS29      2.0065281 -0.58673371  0.15527526
Even1     0.7894929 -0.17073140 -0.30833491
Even2     1.1771369  0.23860382 -0.37746918
Even3     1.4119769  0.23701356 -0.31802890

SData数据

         X.SampleID  Primer         SampleType
CL3             CL3 ILBC_01               Soil
CC1             CC1 ILBC_02               Soil
SV1             SV1 ILBC_03               Soil
M31Fcsw     M31Fcsw ILBC_04              Feces
M11Fcsw     M11Fcsw ILBC_05              Feces
M31Plmr     M31Plmr ILBC_07               Skin
M11Plmr     M11Plmr ILBC_08               Skin
F21Plmr     F21Plmr ILBC_09               Skin
M31Tong     M31Tong ILBC_10             Tongue
M11Tong     M11Tong ILBC_11             Tongue
LMEpi24M   LMEpi24M ILBC_13         Freshwater
SLEpi20M   SLEpi20M ILBC_15         Freshwater
AQC1cm       AQC1cm ILBC_16 Freshwater (creek)
AQC4cm       AQC4cm ILBC_17 Freshwater (creek)
AQC7cm       AQC7cm ILBC_18 Freshwater (creek)
NP2             NP2 ILBC_19              Ocean
NP3             NP3 ILBC_20              Ocean
NP5             NP5 ILBC_21              Ocean
TRRsed1     TRRsed1 ILBC_22 Sediment (estuary)
TRRsed2     TRRsed2 ILBC_23 Sediment (estuary)
TRRsed3     TRRsed3 ILBC_24 Sediment (estuary)
TS28           TS28 ILBC_25              Feces
TS29           TS29 ILBC_26              Feces
Even1         Even1 ILBC_27               Mock
Even2         Even2 ILBC_28               Mock
Even3         Even3 ILBC_29               Mock

Crate an empty list:创建一个空列表:

mt <- cbind(c(1, 1, 2), c(2, 3, 3))
plist <- vector("list", nrow(mt))
names(plist) <- c("p1", "p2", "p3")
st <- c("SampleType")

Now generate the loop with multiples plots: NMDS1 vs NMDS2, NMDS1 vs NMDS3, NMDS2 vs NMDS3:现在生成具有多个图的循环:NMDS1 与 NMDS2、NMDS1 与 NMDS3、NMDS2 与 NMDS3:

for( i in 1:nrow(mt)){
    ax <- t(data.frame(mt[i,]))        
    ORD <- ORDxyz[,ax]
    cnames <- colnames(ORD)
    p <-  ggplot() + geom_point(data=ORD,aes(x=ORD[,1],y=ORD[,2], color=SData[, st]),size=3) +
               geom_vline(xintercept = 0, linetype="dashed", size = 0.5, color= "#999999") +
               geom_hline(yintercept = 0, linetype="dashed", size = 0.5, color= "#999999") +
               labs(x = cnames[1], y = cnames[2], colour=st)
    p <- p + theme_bw()
    plist[[i]] <- p
    print(p)
    Sys.sleep(2)
    
}

the print(p) works well, it show all the plots, the problem is the list of plots (plist), the 3 plots in the plist are the same, all the plots are the last one (NMDS2 vs NMDS3), but the first 2 are lost.!!. print(p) 效果很好,它显示了所有图,问题是图列表(plist),plist 中的 3 个图是相同的,所有图都是最后一个(NMDS2 与 NMDS3),但是前2个都丢了!!!

how cant I fix it???我怎么修不了???

or how can I Combine the 3 plots in a single plot just like:或者我如何将 3 个图组合在一个 plot 中,就像: 在此处输入图像描述

Thanks So Much !!!!非常感谢 !!!!

Here's a possibility using a facet_grid :这是使用facet_grid的一种可能性:

Key Points:关键点:

  1. Combine the two data.frames with dplyr::left_join将两个 data.frames 与dplyr::left_join结合起来
  2. Like Ronak, I used a an approach to make combinations with combn .像 Ronak 一样,我使用了一种方法来组合combn
  3. Use purrr::pmap to iterate over the combinations.使用purrr::pmap迭代组合。
  4. Create a data.frame that contains the first and second variables ( Var1 and Var2 ) along with their values ( x and y )创建一个 data.frame,其中包含第一个和第二个变量( Var1Var2 )以及它们的值( xy
  5. Use facet_grid to have the variables on the two axes.使用facet_grid在两个轴上设置变量。 switch = "both" is optional. switch = "both"是可选的。
  6. Use theme(strip.placement = "outside") to place the facet strips on the outside.使用theme(strip.placement = "outside")将刻面条放在外面。
  7. Use theme(strip.background = element_blank()) to remove the gray background.使用theme(strip.background = element_blank())去除灰色背景。
  8. Use theme(axis.title.x = element_blank()) to remove the axis label.使用theme(axis.title.x = element_blank())删除轴 label。
library(tidyverse)
CombinedData <- ORDxyz %>% rownames_to_column("X.SampleID") %>% left_join(SData)  

data.frame(t(combn(colnames(ORDxyz),2))) %>% 
  pmap_dfr(~data.frame(Var1 = .x, Var2 = .y,
                       x = CombinedData[[.x]], y = CombinedData[[.y]],
                       SampleType = CombinedData[["SampleType"]])) %>%
ggplot() + geom_point(aes(x=x,y=y, color=SampleType),size=3) +
  geom_vline(xintercept = 0, linetype="dashed", size = 0.5, color= "#999999") +
  geom_hline(yintercept = 0, linetype="dashed", size = 0.5, color= "#999999") +
  facet_grid(Var1 ~ Var2) +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) 

在此处输入图像描述

You can use combn to create all possible combination of column names and plot for each combination.您可以使用combn为每个组合创建列名和 plot 的所有可能组合。

library(ggplot2)

combn(names(ORDxyz), 2, function(x) {
  ggplot(ORDxyz) + geom_point(aes(.data[[x[1]]], .data[[x[2]]], color=SData[, st]),size=3) +
    geom_vline(xintercept = 0, linetype="dashed", size = 0.5, color= "#999999") +
    geom_hline(yintercept = 0, linetype="dashed", size = 0.5, color= "#999999") +
    labs(x = x[1], y = x[2], colour=st) + theme_bw()
}, simplify = FALSE) -> plot_list

You can access individual plots using plot_list[[1]] , plot_list[[2]] etc.您可以使用plot_list[[1]]plot_list[[2]]等访问单个图。

To combine them into one plot you can use ggpubr::ggarrange :要将它们组合成一个 plot 您可以使用ggpubr::ggarrange

do.call(ggpubr::ggarrange, c(plot_list, common.legend = TRUE, legend="bottom"))

在此处输入图像描述

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

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