[英]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个都丢了!!!
我怎么修不了???
非常感谢 !!!!
这是使用facet_grid
的一种可能性:
关键点:
dplyr::left_join
结合起来combn
。purrr::pmap
迭代组合。Var1
和Var2
)以及它们的值( x
和y
)facet_grid
在两个轴上设置变量。 switch = "both"
是可选的。theme(strip.placement = "outside")
将刻面条放在外面。theme(strip.background = element_blank())
去除灰色背景。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.