I'm using ggtern to plot a large dataset in a form of tertiary plot (see below an example).
Till a certain data-size everything was perfect, as I was using geom_density_tern(). As I want to visualize a far more complicated dataset loading all of it and rendering with ggplot becomes impossible (limitation on the memory side). I thought that maybe there could be a workaround by imputing the result of kde2d matrix calculated seperately. And that's where I'm stuck. I would like to know if it is possible to do it anyhow in ggtern?
In any case I add a minimal case of the data structure and plotting that I use at this moment.
require(ggplot2)
require(ggtern)
set.seed(1)
mydata <- data.frame(
x = runif(100, min = 0.25, max = 0.5),
y = runif(100, min = 0.1, max = 0.4),
z = runif(100, min = 0.5, max = 0.7))
plot <- ggtern() +
theme_bw() +
theme_hidetitles() +
geom_density_tern(data = mydata,
aes(x = x, y = y, z = z, alpha = ..level.. ),
size = 0.1, linetype = "solid", fill = "blue")+
geom_point(data = mydata,
aes(x = x, y = y, z = z), alpha = 0.8, size = 1)
plot
Those extra lines reproduce the density plot in the ternary coordination system:
library(MASS)
dataTern = transform_tern_to_cart(mydata$x,mydata$y,mydata$z)
dataTernDensity <- kde2d(x=dataTern$x, y=dataTern$y, lims = c(range(0,1), range(0,1)), n = 400)
image(dataTernDensity$x, dataTernDensity$y, dataTernDensity$z)
points(dataTern$x, dataTern$y, pch = 20, cex = 0.1)
segments(x0 = 0, y0 = 0, x1 = 0.5, y1 = 1, col= "white")
segments(x0 = 0, y0 = 0, x1 = 1, y1 = 0, col= "white")
segments(x0 = 0.5, y0 = 1, x1 = 1, y1 = 0, col= "white")
And obtaining this graph:
Thanks in advance for any help!