【问题标题】:How to combine three ternary diagrams on one figure with ggtern如何用ggtern在一个图形上组合三个三元图
【发布时间】:2020-12-09 00:10:19
【问题描述】:

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

【问题讨论】:

    标签: r ggplot2 plot ternary ggtern


    【解决方案1】:

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

    如果您没有原始数据,您可以通过 ggtern 对象: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 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多