Ternary Interpolation / Smoothing
For a long time, people have been sending me requests for a suitable smoothing / contouring / interpolation geometry be made available via ggtern, over and above the Kernel Density function. I am very pleased to say, that the recent Version 1.0.6 has this feature added. Let me demonstrate how it works.
This geometry & stat required an additional mapping, to ‘value’, in order to conduct the interpolation, which by default is done using multivariate linear regression as the mechanism for the interpolation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#Start by setting up the environment library(ggtern) data(Feldspar) theme_base <- function(){ list( theme_bw(), theme(legend.position = c(0,1), legend.justification = c(0,1))) } #Demonstrate the simplest example plot <- ggtern(Feldspar,aes(x=Or,y=An,z=Ab)) + geom_interpolate_tern(aes(value=P.Gpa,color=..level..)) + geom_point() + theme_base() + labs(color="P/Gpa Model") plot |
There are plenty of options to tailor how the interpolation gets done, lets reduce the spacing between the contours, and add some more vibrant colours:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#Demonstrate the use of some options plot <- ggtern(Feldspar,aes(x=Or,y=An,z=Ab)) + geom_interpolate_tern(aes(value= P.Gpa,fill=..level..), colour = "white", formula = value~poly(x,y, degree=2, raw=TRUE), method = "lm", binwidth = 25, buffer = 1.5, n = 200) + geom_point() + theme_base() + scale_fill_gradient(low="green",high="red") + labs(fill="P/GPa Model") plot |
For further information, see the following:
1 2 |
?stat_interpolate_tern ?geom_interpotate_tern |





Comments
Hello,
Just curious,
I can’t seem to find the function:
geom_interpolate_tern() anywhere. It doesn’t appear to be part of ggtern? And the queries
?stat_interpolate_tern
?geom_interpotate_tern
appear to go nowhere. Am I missing something?
thanks for very informative posts!!
best,
Mike
Have you upgraded to Version 1.0.6, it was only accepted by CRAN yesterday…
I wish to supply a function of the three proportions p1, p2 and p3, calculate value of the function at the grid points for a simplex within ggtern and then display the function using a contour plot. I do not see how to compute the function values within ggtern. Could you please suggest a solution. Thanks
I would suggest applying the function to the desired points (mesh) outside of ggtern to produce a dataset of points and their corresponding values, and then supplying this dataset to the geom_interpolate_tern(…) geometry as documented above.
Hello,
I have a requirement to use polygons to show the relationships in my data ( it’s a very small data ). I tried for a whole day using geom_polygon_tern to do this but wasn’t successful. Can you help please.
Can you show me what you have tried so far?
Hi Nicholas,
Basically i was successful in plotting my data on the ternary diagram. What i was trying to add is a polygon to cover the area where the data point appear on the plot. I’ve been able to add this using ppt. but it will be nice if i can do everything directly on R. it sad i can’t attached the final plot so you can see what i’m trying to explain above but here is what i used
“## for tran.tern
#Algal.Mat Sheltered.Inner.Lagoon Foreshore Back.Pool Salicornia.Marsh Tidal.Flat Tidal.Channel
ggtern(data=tran.tern,aes(x=Hyaline,y=Porcelaneous,z=Agglutinated, color=Sub.Environments))+
geom_point(size=10)+
theme_bw()+
theme_hidetitles()”
Hello,
Is there a way to overplot the grid instead of having transparent contour filling?
Thanks you!
If I am understanding your question correctly, you are wishing to change the z-order of the grid, so that it runs over the top of any layers? Unfortunately, this is not possible at the moment.
Trying to install ggtern in R-3.2.3 (64-bit) returns the following error:
* installing *source* package ‘ggtern’ …
** package ‘ggtern’ successfully unpacked and MD5 sums checked
** R
** data
** preparing package for lazy loading
Warning: replacing previous import by ‘grid::arrow’ when loading ‘ggtern’
Warning: replacing previous import by ‘grid::unit’ when loading ‘ggtern’
Warning: replacing previous import by ‘scales::alpha’ when loading ‘ggtern’
Error in get(x, envir = ns, inherits = FALSE) :
object ‘coord_transform.cartesian’ not found
Error : unable to load R code in package ‘ggtern’
ERROR: lazy loading failed for package ‘ggtern’
Is there a fix in the works?
This problem is from the new version of ggplot2, requiring significant changes to ggtern, which I am in the process of working on. Sorry about this, but until I post an update, you wil need to revert to the prior version of ggplot2 (ie 1.1 or whatever it is)
After re-installing R, I can not install ggtern again :
Error in get(x, envir = ns, inherits = FALSE) : object ‘coord_transform.cartesian’ not found
Maybe an issue related to the new version (2.0) of ggplot?
Fabio, totally, Im having to re-write a heap of stuff. Hoping to have this resolved soon. Appologies.
Thanks for your work, and I hope you the best! Keep ggtern rockin’ because is a great piece of software!
Why didn’t you publish this before I finish my PhD 🙂 it would have saved me a lot of time! Wonderful job. You don’t have to answer this question, I am just curious about the interpretation of the results? Is there any formulation recommendation given these parameters?