How to combine three ternary diagrams on one figure with ggtern
I used the following code to plot three ternary diagrams, and want to combine three ternary diagrams (all dots in three ternary diagrams) on one ternary diagram, Keeping the color and position of the dots unchanged:
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()
I suggest you to add a new column for each dataset corresponding to the color
of points and then call it in aesthetics.
If you don't have the raw data, you can get it through ggtern
object : A$data
with A the ternary plot you made in your example.
I did not understand if you also need to keep the same stat_density_tern
, but it is possible by filtering data with the new column color
added.
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))
Created on 2020-12-09 by the reprex package (v0.3.0)