如何使用ggtern将三个三元图组合在一个图上

How to combine three ternary diagrams on one figure with ggtern

如何用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() 

我建议你为每个数据集添加一个新的列,对应于color个点,然后在美学中调用它。

如果你没有原始数据,你可以通过 ggtern object : A$data 和 A 你在你的例子中制作的三元图。

我不明白你是否也需要保持相同 stat_density_tern,但可以通过添加新列 color 过滤数据。

library(tidyverse)
library(ggtern)

set.seed(1)
data.frame(x = runif(100, 0, 1), y = runif(100,0, 0.1), z = runif(100, 0, 0.1), color = "A") %>% 
  bind_rows(data.frame(x = runif(100, 0, 0.1), y = runif(100,0, 0.1), z = runif(100, 0, 1), color = "B")) %>% 
  bind_rows(data.frame(x = runif(100, 0, 0.2), y = runif(100,0, 1), z = runif(100, 0, 0.1), color = "C")) %>% 
  ggtern(mapping = aes(x, y, z = z)) +
  stat_density_tern(geom = 'polygon', n = 400,
                    aes(fill  = ..level.., alpha = ..level..)) +
  geom_point(aes(color = color), shape = 4) + #  map color of the points with the column color
  scale_color_manual("", values = c("A" = "darkblue", "B" = "darkgreen", "C" = "darkred")) + # define colors here
  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(), color = FALSE) + # hide the legend for the color
  theme_rgbg() +
  theme_noarrows() +
  theme(legend.justification = c(0, 1), 
        legend.position      = c(0, 1))

reprex package (v0.3.0)

于 2020-12-09 创建