【问题标题】:create a list plot with ggplot2使用 ggplot2 创建列表图
【发布时间】:2021-01-08 04:43:24
【问题描述】:

我有 2 个数据框,我想用 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 个图组合在一个图中,就像:

非常感谢!!!!

【问题讨论】:

    标签: r ggplot2 plot


    【解决方案1】:

    这里有一个使用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()) 删除轴标签。
    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()) 
    

    【讨论】:

      【解决方案2】:

      您可以使用combn 创建所有可能的列名组合并为每个组合绘制图表。

      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]] 等访问各个图。

      要将它们组合成一个情节,您可以使用ggpubr::ggarrange

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

      【讨论】:

      • 非常感谢,我刚刚对您的代码进行了一些更改,效果很好!!!!
      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多