3

With ggplot2 I normally expect to be able to add a data point like so,

ggtern(df, aes(X, Y, Z, value = VALUE), aes(x, y, z)) +
        geom_point(aes(fill = VALUE), size = 2, stroke = 0, shape = 21) + 
        scale_fill_gradient(low = "red",high = "yellow", guide = F) + 
        scale_color_gradient(low = "red",high = "yellow", guide = F) +
    geom_point(aes(x = 10, y = 10, z = 50), shape = 21)

However, when using the ggtern package to generate a ternary diagram they are being inserted into the wrong locations (see example image) with the following warning:

Warning: Ignoring unknown aesthetics: z

This implies that ggplot2 is likely attempting to render the point and not ggtern. How can I add specific, labeled points to the ggtern plot?

Ternery plot with data point in wrong location

rjzii
  • 14,236
  • 12
  • 79
  • 119
  • 1
    Three things, [1] You have two aes arguments in the ggtern constructor [2] Your intended point has a composition which adds to 70, and is equivalent to `x=0.143, y=0.143 z=0.714`, exactly where the point was placed, [3] the warning `Warning: Ignoring unknown aesthetics: z` is an anomaly to do with ggplot2 trying to handle the x,y,z aesthetics, ignore it. – Nicholas Hamilton May 22 '18 at 08:49
  • On another note, can I also suggest you look at the hexbin or tribin geometries in the latest development version, which will be cleaner and more efficient than plotting many coloured points like you have done. – Nicholas Hamilton May 22 '18 at 08:58
  • @NicholasHamilton Thanks for the tip! The tribin looks like it might work for the data set, or failing at that, a second column in the same dataset. – rjzii May 22 '18 at 15:28

2 Answers2

0

It appears that there are two main points for this. The first is to create an annotation, although that might not be ideal since it is not as precise as a point. An example would be,

ggtern() + 
annotate(geom  = 'text',
              x     = c(0.5,1/3,0.0),
              y     = c(0.5,1/3,0.0),
              z     = c(0.0,1/3,1.0),
              angle = c(0,30,60),
              vjust = c(1.5,0.5,-0.5),
              label = paste("Point",c("A","B","C")),
              color = c("green","red",'blue')) +
  theme_dark() + 
  theme_nomask()

enter image description here

The second option would be to create a new data frame and add that to the plot. While this has the advantage of having more control over the point, the disadvantage is that labeling will require additional work.

rjzii
  • 14,236
  • 12
  • 79
  • 119
0

One possibility is to have a column that identifies the points you wish to label, in this example the column 'lab' and say I want to label points one and three:

df <- data.frame(x=c(10,20,30), y=c(15,25,35), z=c(75,55,35), VALUE=c(1,2,3), lab=c("One", "", "Three"))

Then geom_text or geom_label can be used to label those specific points, for example:

ggtern(df, aes(x, y, z, value = VALUE)) +
  geom_point(aes(fill = VALUE), size = 2, stroke = 0, shape = 21) + 
  scale_fill_gradient(low = "red",high = "yellow", guide = F) + 
  scale_color_gradient(low = "red",high = "yellow", guide = F) +
  geom_text(aes(label = lab), vjust=1)
John Walker
  • 191
  • 5