首页 > 解决方案 > 如何用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() 

标签: rggplot2plotternaryggtern

解决方案


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

如果您没有原始数据,您可以通过ggternobject 获取它: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 包(v0.3.0)于 2020 年 12 月 9 日创建


推荐阅读