'm using ggplot2 to plot two variables at a time. I'm plotting two age groups 
and overall data on the same graph. I'm also using stat_cor form the ggpubr 
package to report correlations for the two groups and overall data.

I want each stat_cor label to have a custom subscript - the group name ("old", 
"young"). I have managed to do this for the overall data, but I don't know how 
to add custom labels for each group separately. I'd like the labels to look 
like this: https://imgur.com/a/naF7uNW

> for (i in 18:21) {
  p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), 
color=factor(AgeGroup))) + 
    geom_smooth(method="lm") + 
    geom_point(size = 4) +
    geom_smooth(data=df, mapping = aes(x = Age, y = unlist(df[i]), group=1, 
color="black"), method = "lm") +
    scale_colour_discrete(name="Group", labels=c("young", "old", "overall")) + 
    stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i]))) +
    stat_cor(aes(x = Age, y = unlist(df[i]), group=1, color="black", 
                 label = paste(sub("R",expression("R"[overall]),..r.label..), 
..p.label.., sep = "~`,`~")))
  
  ggsave(p1, file=paste0("Age_", names(df)[i], ".png"), scale=1)
}
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to