using ggplot and ggtern take string from variables in the dataframe to label arrows

46 Views Asked by At

using ggplot2 with the extension ggtern I need to dynamically change the arrow labels based on the facet.

Starting off with

df <- tibble::tribble(
  ~Tpoints, ~Lpoints, ~Rpoints,                    ~combo,
       0.4,      0.5,      0.1, "1_score-2_score-3_score",
       0.3,      0.2,      0.5, "1_score-2_score-3_score",
       0.5,      0.3,      0.2, "1_score-2_score-3_score",
       0.5,      0.4,      0.1, "1_score-2_score-3_score",
       0.4,      0.2,      0.4, "1_score-2_score-3_score",
       0.1,      0.9,        0, "1_score-2_score-4_score",
       0.5,      0.2,      0.3, "1_score-2_score-4_score",
       0.2,        0,      0.8, "1_score-2_score-4_score",
       0.5,      0.1,      0.4, "1_score-2_score-4_score",
       0.5,      0.3,      0.2, "1_score-2_score-4_score",
       0.4,      0.5,      0.1, "1_score-2_score-5_score",
       0.6,      0.3,      0.1, "1_score-2_score-5_score",
       0.2,      0.6,      0.2, "1_score-2_score-5_score",
         0,      0.5,      0.5, "1_score-2_score-5_score",
       0.5,      0.5,        0, "1_score-2_score-5_score",
       0.1,      0.9,        0, "1_score-3_score-4_score",
       0.5,      0.4,      0.1, "1_score-3_score-4_score",
       0.2,      0.2,      0.6, "1_score-3_score-4_score",
       0.1,      0.1,      0.8, "1_score-3_score-4_score",
       0.2,      0.1,      0.7, "1_score-3_score-4_score"
  ) |> 
  tidyr::separate(combo, into = c("A", "B", "C"), sep = "-", remove = FALSE)


PLOT <- ggtern::ggtern(data=df, ggplot2::aes(x = Lpoints, y = Tpoints, z = Rpoints))
PLOT +
  ggtern::theme_showarrows() +
  ggplot2::facet_wrap(~combo)

Plain Plot

I need the arrow labels to be a string taken from the dataframe to get a plot like this

Desired

I have tried accessing information in the dataframe from ggtern::Tlab(A). I tried using glue syntax ggtern::Tlab("{A}") and I have tried manually slamming the labels on by assigning to the plot object PLOT$labels$Larrow <- c("1_score", "2_score")

0

There are 0 best solutions below