How to modify the ternary diagrams with ggtern
I used the following code to plot the ternary diagrams, and want to make some changes:
library(ggtern) set.seed(1) plot <- ggtern(data = data.frame(x = runif(100), y = runif(100), z = runif(100)), aes(x, y, z)) plot + stat_density_tern(geom = 'polygon', n = 400, aes(fill = ..level.., alpha = ..level..)) + geom_point() + theme_rgbg() + theme(legend.justification=c(0,1), legend.position=c(0,1)) + #theme_gridsontop() + labs(title = "Example Density/Contour Plot") + scale_fill_gradient(low = "blue",high = "red") + # scale_color_gradient(low="yellow",high="red") + guides(fill = guide_colorbar(order=1),color="none")
Try the following modifications:
labels =
in each of the three continuous scales used in ggtern
. These are scale_L_continuous
for the left-sided edge, scale_R_continuous
for the right edge, and scale_T_continuous
for the bottom edge. Set the breaks to c(0, 0.2, 0.4, 0.6, 0.8, 1)
(or more succinctly with 0:5 / 5
), and set labels
to the same values.theme_noarrows()
to your plot.guides(alpha = guide_none())
labels
as an argument to scale_fill_gradient
. Since there are 5 breaks (1:5), we pass five labels: c("low", "", "", "", "high")
Here's a full reprex. If you cut and paste exactly this code in a fresh R session you should get exactly the same plot:
library(ggtern)
set.seed(1)
ggtern(data = data.frame(x = runif(100), y = runif(100), z = runif(100)),
mapping = aes(x, y, z = z)) +
stat_density_tern(geom = 'polygon', n = 400,
aes(fill = ..level.., alpha = ..level..)) +
geom_point() +
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))
Created on 2020-11-24 by the reprex package (v0.3.0)