0

Good afternoon ,

Assume we have the following long data :

dput(q1[[2]] %>%  group_by_(~ Var2) %>%  do(head(., n = 5)))

melted=structure(list(Var1 = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), Var2 = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
10L, 10L, 11L, 11L, 11L, 11L, 11L), .Label = c("Sensitivity", 
"Specificity", "Pos Pred Value", "Neg Pred Value", "Precision", 
"Recall", "F1", "Prevalence", "Detection Rate", "Detection Prevalence", 
"Balanced Accuracy "), class = "factor"), value = c(0, 0.85, 
0.85, 0.86, 0.86, 0, 0.188235294117647, 0.188235294117647, 0.188235294117647, 
0.188235294117647, 0, 0.711297071129707, 0.711297071129707, 0.713692946058091, 
0.713692946058091, 0, 0.347826086956522, 0.347826086956522, 0.363636363636364, 
0.363636363636364, 0, 0.711297071129707, 0.711297071129707, 0.713692946058091, 
0.713692946058091, 0, 0.85, 0.85, 0.86, 0.86, 0, 0.774487471526196, 
0.774487471526196, 0.780045351473923, 0.780045351473923, 0, 0.701754385964912, 
0.701754385964912, 0.701754385964912, 0.701754385964912, 0, 0.596491228070175, 
0.596491228070175, 0.603508771929825, 0.603508771929825, 0, 0.83859649122807, 
0.83859649122807, 0.845614035087719, 0.845614035087719, 0, 0.519117647058823, 
0.519117647058823, 0.524117647058824, 0.524117647058824)), row.names = c(NA, 
-55L), groups = structure(list(Var2 = structure(1:11, .Label = c("Sensitivity", 
"Specificity", "Pos Pred Value", "Neg Pred Value", "Precision", 
"Recall", "F1", "Prevalence", "Detection Rate", "Detection Prevalence", 
"Balanced Accuracy "), class = "factor"), .rows = structure(list(
    1:5, 6:10, 11:15, 16:20, 21:25, 26:30, 31:35, 36:40, 41:45, 
    46:50, 51:55), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

  melted <- melted %>%
    mutate(Label = case_when(Var1 ==  round(v/16) ~ Var2)) 
  
 ggplot(data = melted, aes(Var1, value,  group = Var2)) +
    geom_line(aes(color = Var2), size = 1.2) +
    geom_label(aes(label = Label), nudge_x = 0.35, size = 4) 

This gives the following plot :

enter image description here

I'm wanting to add groups labels at the end of curves ( "Sensitivity", "Specificity", "Pos Pred Value", "Neg Pred Value" , ...)

Thank you for help !

Tou Mou
  • 1,270
  • 5
  • 16

1 Answers1

0

Here is one way using ggrepel library -

library(ggrepel)
library(dplyr)
library(ggplot2)

melted %>%
  mutate(Label = as.character(Var2)) %>%
  group_by(Var2) %>%
  mutate(Label = replace(Label, -n(), NA)) %>%
  ungroup %>%
  ggplot(aes(Var1, value,  group = Var2)) +
  geom_line(aes(color = Var2), size = 1.2) +
  geom_label_repel(aes(label = Label),nudge_x = 1,na.rm = TRUE) +
  guides(color=FALSE) + 
  scale_x_continuous(limits = c(1, 5.5))

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213