0
votes

I have 2 data frame that I want to plot with ggplot2 (geom_point) to create a list of plots

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:

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 !!!.

how cant I fix it ???

or how can I Combine the 3 plots in a single plot just like : enter image description here

Thanks So Much !!!!

2

2 Answers

2
votes

Here's a possibility using a facet_grid:

Key Points:

  1. Combine the two data.frames with dplyr::left_join
  2. Like Ronak, I used a an approach to make combinations with combn.
  3. Use purrr::pmap to iterate over the combinations.
  4. Create a data.frame that contains the first and second variables (Var1 and Var2) along with their values (x and y)
  5. Use facet_grid to have the variables on the two axes. switch = "both" is optional.
  6. Use theme(strip.placement = "outside") to place the facet strips on the outside.
  7. Use theme(strip.background = element_blank()) to remove the gray background.
  8. Use theme(axis.title.x = element_blank()) to remove the axis 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()) 

enter image description here

1
votes

You can use combn to create all possible combination of column names and plot for each combination.

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.

To combine them into one plot you can use ggpubr::ggarrange :

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

enter image description here