如何用ggtern在一个图形上组合三个三元图
我用下面的代码绘制了三个三元图,想把三个三元图(三个三元图中的所有点)组合在一个三元图上,保持点的颜色和位置不变:
library(ggtern)
set.seed(1)
A = ggtern(data = data.frame(x = runif(100, 0, 1), y = runif(100,0, 0.1), z = runif(100, 0, 0.1)),
mapping = aes(x, y, z = z)) +
stat_density_tern(geom = 'polygon', n = 400,
aes(fill = ..level.., alpha = ..level..)) +
geom_point(shape = 4, color = "darkblue") +
scale_fill_gradient(low = "blue", high = "red", name = "", breaks = 1:5,
labels = c("low", "", "", "", "high")) +
scale_L_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
scale_R_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
scale_T_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
# labs(title = "Example Density/Contour Plot") +
guides(fill = guide_colorbar(order = 1), alpha = guide_none()) +
theme_rgbg() +
theme_noarrows() +
theme(legend.justification = c(0, 1),
legend.position = c(0, 1))
B = ggtern(data = data.frame(x = runif(100, 0, 0.1), y = runif(100,0, 0.1), z = runif(100, 0, 1)),
mapping = aes(x, y, z = z)) +
stat_density_tern(geom = 'polygon', n = 400,
aes(fill = ..level.., alpha = ..level..)) +
geom_point(shape = 4, color = "darkgreen") +
scale_fill_gradient(low = "blue", high = "red", name = "", breaks = 1:5,
labels = c("low", "", "", "", "high")) +
scale_L_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
scale_R_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
scale_T_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
# labs(title = "Example Density/Contour Plot") +
guides(fill = guide_colorbar(order = 1), alpha = guide_none()) +
theme_rgbg() +
theme_noarrows() +
theme(legend.justification = c(0, 1),
legend.position = c(0, 1))
C = ggtern(data = data.frame(x = runif(100, 0, 0.2), y = runif(100,0, 1), z = runif(100, 0, 0.1)),
mapping = aes(x, y, z = z)) +
stat_density_tern(geom = 'polygon', n = 400,
aes(fill = ..level.., alpha = ..level..)) +
geom_point(shape = 4, color = "darkred") +
scale_fill_gradient(low = "blue", high = "red", name = "", breaks = 1:5,
labels = c("low", "", "", "", "high")) +
scale_L_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
scale_R_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
scale_T_continuous(breaks = 0:5 / 5, labels = 0:5/ 5) +
# labs(title = "Example Density/Contour Plot") +
guides(fill = guide_colorbar(order = 1), alpha = guide_none()) +
theme_rgbg() +
theme_noarrows() +
theme(legend.justification = c(0, 1),
legend.position = c(0, 1))
layout_matrix <- matrix(c(4, 1, 1, 4,2, 2, 3, 3), nrow = 2, byrow = TRUE)
pdf("test.pdf", width = 11, height = 9) # Open a new pdf file
grid.arrange(A,B,C,A, layout_matrix = layout_matrix)
dev.off()