Plotting the co-ordinates of the compositional mean on a ternary plot with ggtern

58 Views Asked by At

I have a compositional set of data investigating sedentary behaviour, sleep and physical activity. Each of these data points is a proportion of time. I want to plot this on a ternary plot using ggtern and have done as follows which results in the below output.

library(ggtern)
library(gtools)
library(zCompositions)

comp_df <- as.data.frame(data[,c("id","PA", "SB", "Sleep")])

ggtern(data = comp_df, aes(Sleep, SB, PA)) +
  geom_mask() +
  geom_point() +
  theme_bw() + 
  theme_showarrows() +
  geom_confidence_tern(colour = "black", breaks = c(0.25, 0.5, 0.75)) 

enter image description here

Now this is most of the way there. However I want to add a data point that plots the compositional mean of this dataset on the ternary plot, and also labels the co-ordinates of this mean in a specific way. I've use the epicoda package from GitHub to identify the compositional mean and add the appropriate labels as below.

comp_labels <- c("Sleep", "SB", "PA")
library(epicoda)
cm <- comp_mean(comp_df, comp_labels = comp_labels, units = "hr/day")

Then, I can add this easily to my ternary plot with the addition of another geom_point() argument which results in the same output but with the additional point for the compositional mean.

ggtern(data = comp_df, aes(Sleep, SB, PA)) +
  geom_mask() +
  geom_point() +
  geom_point(data = cm) +
  theme_bw() + 
  theme_showarrows() +
  geom_confidence_tern(colour = "black", breaks = c(0.25, 0.5, 0.75)) 

What I want to be able to do is add lines to label the mean with both percentages and time like this image. Any assistance here would be very much appreciated.

enter image description here

1

There are 1 best solutions below

0
thothal On

Unfortunately you did not provide any data, so this code comes untested, but geom_[TLR]line should help you. For example to mark the mixture 20%/20%/60% you could use:

ggtern(data = comp_df, aes(Sleep, SB, PA)) +
  geom_mask() +
  geom_point() +
  theme_bw() + 
  theme_showarrows() +
  geom_confidence_tern(colour = "black", breaks = c(0.25, 0.5, 0.75)) +
  geom_Tline(Tintercept = .2) +
  geom_Lline(Lintercept = .2) +
  geom_Rline(Rintercept = .6)

That is, something like:

geom_Tline(Tintercept = cm$SB) +
  geom_Lline(Lintercept = cm$Sleep) +
  geom_Rline(Rintercept = cm$PA)

should do the trick.