I have this data file that has enough data points for me to plot a "heatmap" in ternary plot. (It is not really heat map, just a scatter plot with enough data points)
library(ggtern)
library(reshape2)
N=90
trans.prob = as.matrix(read.table("./N90_p_0.350_eta_90_W12.dat",fill=TRUE))
colnames(trans.prob) = NULL
# flatten trans.prob for ternary plot
flattened.tb = melt(trans.prob,varnames = c("x","y"),value.name = "W12")
# delete rows with NA
flattened.tb = flattened.tb[complete.cases(flattened.tb),]
flattened.tb$x = (flattened.tb$x-1)/N
flattened.tb$y = (flattened.tb$y-1)/N
flattened.tb$z = 1 - flattened.tb$x - flattened.tb$y
ggtern(data = flattened.tb, aes(x=x,y=y,z=z)) +
geom_point(size=1, aes(color=W12)) +
theme_bw() +
scale_color_gradient2(low = "green", mid = "yellow", high = "red")
Here is what I got:
I want to get something like the following using ggtern
:
My question is: How can I get something like the second figure using ggtern
?
Edit 1: Sorry for the typo in the file name. I fixed the filename. The data file contains too much data points for me to directly paste them here.
The 2nd figure was produced by a 3rd-party Matlab package ternplot
. I want a ternary contour plot that has discrete lines rather than the heatmap in my first figure. To be more specific, I want to specify a list of contour lines such as W12=0.05,0.1,0.15,...
. I have played with geom_density_tern
and geom_interpolate_tern
for hours but still have no clue how to get what I want.
The MATLAB code is:
[HCl, Hha, cax] = terncontour(X,Y,1-X-Y,data,[0.01,0.1,0.2,0.3,0.4,0.5]);
where X,Y,1-X-Y
specify the coordinate on the plot, data
stores the values and the vector specifies the values of the contours.
This doesn't look as beautiful as your example, but hopefully it gets you significantly closer to where you want to be:
Plot just needs a prettying up. I can't say which data regions will look best for plotting.
WDG, I have made a few small changes to ggtern, for better handling this type of modelling, which has just been submitted to CRAN, so should be available over the next day or so. In the interim, you can download from source from my BitBucket account: https://bitbucket.org/nicholasehamilton/ggtern
Anyway, here is the source, which will work from ggtern version 2.1.2.
I have included the points underneath (with a mild alpha value) so one can observe how representative the interpolation geometry has been:
This produces the following output:
For the sake of producing a diagram closer to the colours and orientation as the sample matlab contour plot, try the following:
This produces the following output: