rlegendannotate

Adding manual legend for GGplot with calculated values


First reprex for me here. Couldn't add library(ggplot); hope thats fine. I created a Bandman-Altman Plot. Now I would like to have the calculated numbers from Mean, Sd, Upper and Lower-Limits written on a legend, something like this:

Legend should appear like this example.

Is it only possible with scale_color_manual()?

Thanks for help

df <- data.frame(A=c(494, 395, 516, 434, 476,
                     557, 413, 442, 650, 433,
                     417, 656, 267, 478, 178,
                     423, 427),
                 B=c(512, 430, 520, 428, 500,
                     600, 364, 380, 658, 445,
                     432, 626, 260, 477, 259, 
                     350, 451))

#create new column for average measurement
df$avg <- rowMeans(df) 

#create new column for difference in measurements
df$diff <- df$A - df$B

#mean difference
mean_diff <- mean(df$diff)

#- 95% confidence interval limits
lower <- mean_diff - 1.96*sd(df$diff)
upper <- mean_diff + 1.96*sd(df$diff)

#create Bland-Altman plot
ggplot(df, aes(avg, diff)) +
  geom_point(size=3) +
  geom_hline(yintercept = mean_diff,, linetype="dashed") +
  geom_hline(yintercept = 0) +
  geom_hline(yintercept = lower, color = "red", linetype="dashed") +
  geom_hline(yintercept = upper, color = "red", linetype="dashed") +
  ggtitle("Bland-Altman Plot") +
  annotate("text", label ="Upper Limit", y=upper, x = 600) +
  annotate("text", label ="Lower Limit", y=lower, x = 600) +
  annotate("text", label ="Mean", y=mean_diff, x = 600) +
  ylab("Difference Between Measurements") +
  xlab("Average Measurement")
#> Error in ggplot(df, aes(avg, diff)): konnte Funktion "ggplot" nicht finden

Output for code above

Created on 2024-10-18 with reprex v2.1.1


Solution

  • You cannot create the type of legend you are looking for with a built-in ggplot scale. This type of legend must be created manually using annotate:

    ggplot(df, aes(avg, diff)) +
      geom_point(size = 3) +
      geom_hline(yintercept = mean_diff,, linetype="dashed") +
      geom_hline(yintercept = 0) +
      geom_hline(yintercept = lower, color = "red", linetype="dashed") +
      geom_hline(yintercept = upper, color = "red", linetype="dashed") +
      ggtitle("Bland-Altman Plot") +
      annotate("text", label ="Upper Limit", y=upper, x = 600, vjust = -0.5) +
      annotate("text", label ="Lower Limit", y=lower, x = 600, vjust = -0.5) +
      annotate("text", label ="Mean", y=mean_diff, x = 600, vjust = 1.5) +
      ylab("Difference Between Measurements") +
      xlab("Average Measurement") +
      theme_bw() +
      annotate(geom = "text", x = 500, y = 50, hjust = 0, parse = TRUE,
               label = deparse(bquote(Mean[diff] == .(mean_diff)))) +
      annotate(geom = "text", x = 500, y = 45, hjust = 0, parse = TRUE,
               label = deparse(bquote(SD[diff] == .(sd(df$diff))))) +
      annotate(geom = "text", x = 500, y = 40, hjust = 0, parse = TRUE,
               label = deparse(bquote(LL == .(lower)))) +
      annotate(geom = "text", x = 500, y = 35, hjust = 0, parse = TRUE,
               label = deparse(bquote(UL == .(upper))))
    

    enter image description here