# ggtern 2.0 now available

Recently ggplot2 received a severe makeover by releasing version 2.0, and in the spirit of improvement, I thought ggtern should also get an overhaul, so after a few-hundred hours of code review, here is what has changed:

### Theme elements:

Previously, the nomenclature scheme for the new theme elements was a bit all over the shop, so the theme elements have been renamed. Every custom theme element for this package begins with ‘tern.XXX’ such as ‘tern.panel.background’ or ‘tern.axis.clockwise’, for the full list of new theme elements, see the help file: ?theme_elements

### Approved Geometries:

ggplot2 v2.0 has many new geometries, and not all of them are relevant to ggtern, so like before, this package has an opt-in policy where only certain geometries are approved: geom_point, geom_path, geom_line, geom_label geom_text, geom_jitter, geom_polygon, geom_segment, geom_count and geom_blank.

### New Geometries:

ggtern 2.0 carries over the existing geometries, and also ads a few more, the full list is as follows: geom_Tline, geom_Rline, geom_Lline, geom_errorbarT, geom_errorbarL, geom_errorbarR, geom_density_tern, geom_confidence_tern, geom_mask, geom_smooth_tern, geom_Tisoprop, geom_Lisoprop, geom_Risoprop and geom_interpolate_tern

### Stats and Positions:

The full list of approved stats are as follows: stat_identity, stat_confidence, stat_density_tern, stat_smooth_tern, stat_sum, stat_unique, and stat_interpolate_tern whilst the approved positions are as follows: position_identity, position_nudge_tern and position_jitter_tern

### Rebuilt Plot Arrangement Routine

The ggptern plot assembly routine has been completely re-written, the layout is much more predictable, one of the main achievements was providing the ability to rotate and shift the diagrams quite easily, here is an example:

1 2 3 4 5 6 7 8 9 10 11 |
data(Feldspar) arrangement = list() for(r in seq(0,60,by=20)){ x = ggtern(data=df,aes(Ab,An,Or)) + geom_point() + theme_showarrows() + theme_rotate(r) + ggtitle(sprintf("%i Degrees",r)) arrangement[[length(arrangement) + 1]] = x } grid.arrange(grobs = arrangement) |

### Use of Orthonormal Basis

Several of the routines use isometric-logratio basis transformations, here is an example, using a new data-set included in the package, here we shall demonstrate the implementation of the new clipping mask, and two variants of the density estimate, the first applying density on the cartesian space, and the (now) default, isometric-logratio basis.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
data(Fragments) arrangement = list() for(base in c('identity','ilr')){ x = ggtern(Fragments,aes(Qm,Qp,M)) + stat_density_tern(geom='polygon', aes(fill=..level..), base=base, ###NB Base Specification colour='grey50') + theme_dark() + geom_point() + ggtitle(sprintf("Basis: %s",base)) + scale_fill_gradient(low='green',high='red') + limit_tern(.5,1,.5) arrangement[[length(arrangement) + 1]] = x } grid.arrange(grobs = arrangement,nrow=1) |

### Additional Labels

There are a few more labels and convenience functions which can be applied for additional customisation, here let us demonstrate the interpolation geometry, isolines, the automatic latex format parsing, the different labels for the precession arrows to the apex labels, and the legend positioning convenience function:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
data(Feldspar) x = ggtern(Feldspar,aes(Ab,An,Or,value=T.C)) + stat_interpolate_tern(geom="polygon", formula=value~x+y, method=lm,n=100, breaks=seq(0,1000,by=100), aes(fill=..level..),expand=1) + theme_rgbw() + theme_showarrows() + theme_legend_position('topleft') + labs(title = "Elkins and Grove", fill = "Temperature", Tarrow = "\\textbf{Anorthite} CaAl_2Si_2O_8", Larrow = "\\textbf{Albite} NaAlSi_3O_8", Rarrow = "\\textbf{Orthoclase} KAlSi_3O_8") + geom_point(aes(shape=Feldspar,colour=Feldspar)) + geom_Tisoprop(value=0.5,colour='darkred') + geom_Lisoprop(value=0.5,colour='darkblue') + geom_Risoprop(value=0.5,colour='darkgreen') + weight_percent() |

### A Final Note

So the above is some of the new features, if you have any feedback, post a comment immediately below, the user manual can be found HERE. I probably spent a couple-of-hundred hours preparing this release, essentially re-writing the entire package to work with the new ggplot2 version, so, if this package makes your life easier and produces diagrams of publication quality, any kind of donation would be highly appreciated.

Thanks for all your work. It makes my days. Fabio

No worries. Glad you like it.

Thanks for the update! Could you give a hint how to put greek letters in the LaTeX expressions (for example in the labels). I tried with \\ and \, and it doesn’t see it.

You need to put them inside ‘math’ type, ie, try:

Works like charm!

Yeah, latex2exp package is awesome. Makes working with expressions easy…

Thank you for your hard work! May I know how to plot tie-lines with this? For example, I have a solubility curve like so: http://www.scielo.br/img/fbpe/bjce/v19n2/10666f2.gif and want to draw a line through a point in the center and two points on the side that fit on the solubility curve. Only the value of the z axis on both sides are known, and the lines must terminate there. How can I do that with ggtern? Thanks for your help!

If you can supply the data (ie x,y,z,xend,yend,zend) yourself, use geom_segment(…). If you can provide a sample of your data, I’ll put together an example — if you don’t mind me using it on this site as a demonstration to assist others.

Hi Nicholas,

First of all thank you so much for this amazing package!

I´ve one question. With the previous version I was applying GAMs to smooth parameters in ternary plots. However, since the new version 2.0 (which I really like though) my script does not work anymore.

I also tried to use GAMs and GLMs using this example code:

However, changing the method from “lm” to “gam” or “glm” does not change anything. Also with the old version I was able to specify formulas as value = x+y+z (since the ternary plot has 3 axis). But since this version I get an error as soon as I include z in the model formula.

Thanks so much for your help!

Sorry I was under serious time pressure to get the new package functional with the latest ggplot2 update. Gam works, you just need to load the package (library(gam)). At the moment in the latest release, the interpolation geometry functions on the log-ratio space, which reduces the three axes (x,y,z) into an unbound two dimensional system (x, y). It appears as though nothing is changing, because the model is working on the log ratio space, prior to being transformed back, however, if you change the formula (to say

`poly(x,y,degree=2)`

and the value of`expand`

(to say 0), clear differences start to emerge. With regards to the use of the`z`

variable, even though the functionality you are referring to was provided in earlier versions of ggtern, some people might argue that generating a linear model in terms of the three constituents of a simplex is a flawed approach, nontheless, I take your request on board, and will provide a means to change the ‘base’ of the model, just like what can be done in the stat_density_tern(…). Thanks for your inquiry.Thanks so much for your fast and helpfull reply!

The gam is now working fine for me.

Keep up the good work!

Hi Nicholas,

thanks for the great package!

I’m wondering if I need to do anything special to make the theme elements work when using facet_grid.

The following code additions (I used silly values as a test) work fine with the basic example:

plot <- ggtern(data=Feldspar,aes(Ab,An,Or)) + geom_point() +

xlab("ABC") + ylab("DEF") + zlab("GHI") +

theme_minimal() + theme_showarrows() + theme(legend.position="none",

tern.axis.title = element_text(size = 4),

tern.axis.text = element_text(size = 48))

When I add the theme-related code bits to my code that uses facet_grid, the legend is successfully suppressed, but the text sizes don't change.

Thanks for any hints.

Thomas, The theme elements works for me under facet_grid(…), can you email me (nick@ggtern.com) with an exact example that reproduces your problem? Cheers. N

Thank you for the update! Quick question: previously, plotted elements (lines, points, …) were plotted over the axes (e.g., the first ternary diagram in http://www.ggtern.com/usage-basic/), but now, the axes clip the plotted elements. Is it possible to plot over the axes again?

Great work, Nicholas –

I love the new theme_rotate() function, thank you so much for adding this into ggtern 2.0. Also, FYI your donation link doesn’t seem to be working properly…

Caitlin, thanks, no problem. The donate button works fine for me, give it a go if you feel so inclined… 😉

Hi Nicholas,

Thank you for creating this package. It is very valuable for the figure I’m creating. I’m having trouble modifying the legend and I’m wondering if you can help. Specifically, I’d like to specify the numbers at which the tick marks appear in the legend.

Also, I’m using scale_fill_gradientn() for the colors and I’m wondering if there is a way to specify the value that will be set at the minimum and maximum for this function and for the legend. I’ve tried limits, but haven’t had luck.

Thank you, Mikey

Hi Nicholas,

Congrats for this great package — I feel like there is a 1000 ways of using it for my lectures and for my research!!

Do you think you could help me code a division of the resulting triangle into differently colored zones? I am thinking about something like the graph below without the need to edit in outside of RStudio.

https://www.dropbox.com/s/mzgdbaadvr64kdn/Triangle_plot.png?dl=0

Thank you in advance!

Nico

Very very nice tool! Congrats! You should provide a bib–text reference to include your package in the references of articles!

Congrats once again!

Hi G, thanks very much. This is actually addressed on the first page of the online documentation. See HERE for further information.

Hi

Totally awesome, thanks a million

I run into an issue with latest version while trying to use

geom_interpolate_tern

Apparently this is

Error: GeomInterpolateTern was built with an incompatible version of ggproto.

Please reinstall the package that provides this extension.

Do you have any idea?

Yep sorry about this, compatability issues with the latest ggplot version most likely. You’ll have to revert to ggplot2 2.1.x until I can get time to address this. Apologies.

Many thanks for quick reply will try this and get back to you

Sweat back working TA