簡體   English   中英

使用 ggplot2 創建列表 plot

[英]create a list plot with ggplot2

我有 2 個數據框,我想用 plot 和 ggplot2 (geom_point) 創建一個繪圖列表

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

數據

         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

創建一個空列表:

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

現在生成具有多個圖的循環: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)
    
}

print(p) 效果很好,它顯示了所有圖,問題是圖列表(plist),plist 中的 3 個圖是相同的,所有圖都是最后一個(NMDS2 與 NMDS3),但是前2個都丟了!!!

我怎么修不了???

或者我如何將 3 個圖組合在一個 plot 中,就像: 在此處輸入圖像描述

非常感謝 !!!!

這是使用facet_grid的一種可能性:

關鍵點:

  1. 將兩個 data.frames 與dplyr::left_join結合起來
  2. 像 Ronak 一樣,我使用了一種方法來組合combn
  3. 使用purrr::pmap迭代組合。
  4. 創建一個 data.frame,其中包含第一個和第二個變量( Var1Var2 )以及它們的值( xy
  5. 使用facet_grid在兩個軸上設置變量。 switch = "both"是可選的。
  6. 使用theme(strip.placement = "outside")將刻面條放在外面。
  7. 使用theme(strip.background = element_blank())去除灰色背景。
  8. 使用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()) 

在此處輸入圖像描述

您可以使用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

您可以使用plot_list[[1]]plot_list[[2]]等訪問單個圖。

要將它們組合成一個 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