0

I have a line plot for 3 different groups and I would like to present on the chart, in addition to the significant differences, the significance of the differences between subgroups. For example, add the significance of the difference between each of the 3 populations' weights at age 1. I saw the stat_compare_means() function but failed to use it vertically between lines in my chart. my current code:

library(tidyverse)
#pivot data into long format

df <- data.frame(
  stringsAsFactors = FALSE,
                ID = c(1L, 2L, 3L, 4L, 5L),
        POPULATION = c("A", "A", "B", "B", "C"),
   weight.at.age.1 = c(13.37, 5.19, 7.68, 6.96, 10.35),
   weight.at.age.2 = c(14.15, 15.34, 6.92, 15.12, 8.86),
   weight.at.age.3 = c(17.36, NA, 19.42, 36.39, 26.33)
) %>% 
  pivot_longer(cols = weight.at.age.1:weight.at.age.3, 
               names_to = 'age', 
               values_to = 'weight') %>% 
  mutate(age = str_remove(age, 'weight.at.age.'))

#plot data
ggline(data = df, 
       mapping = aes(x = age, 
                     y = weight, add = "mean_se", color=POPULATION))+
  stat_compare_means(aes(group = POPULATION), method = "anova", label = "p.signif", 
                     label.y = c(56))

Tnx!

Maya Eldar
  • 49
  • 5
  • The stat_compare_means() function documentations says : "Add mean comparison p-values to a ggplot, such as box blots, dot plots and stripcharts". That must be the reason it didn't work out. And by looking at your data, what you want to do is more like a 2-way-anova instead of just anova. In that case, I would make the test and then try to manually put the significance marks manually with geom_text or somenthing like that. – bpvalderrama Jun 12 '22 at 17:29
  • I took this list of ggplots options as an example... but, I saw that error: `Error in f(...) : Can only handle data with groups that are plotted on the x-axis`. so I guss Im looking for a function similar to `stat_compare_means` that can be applied on groups on the y-axis.. – Maya Eldar Jun 14 '22 at 11:33

0 Answers0