[R] How to differ stats_cor labels by group on a ggplot

Paulina Skolasinska p@u||n@@@ko| @end|ng |rom hotm@||@com
Sun Jul 12 03:20:02 CEST 2020


Sorry for misspelling your name, Jim. Well, it seems this is not worth the effort then. If my advisor decides this is absolutely essential, I�ll add it in gimp or something. Thanks for giving it a go, Jim.

Get Outlook for iOS<https://aka.ms/o0ukef>
________________________________
From: Jim Lemon <drjimlemon using gmail.com>
Sent: Saturday, July 11, 2020 6:32:13 PM
To: Paulina Skolasinska <paulina.skol using hotmail.com>
Cc: r-help using r-project.org <r-help using r-project.org>
Subject: Re: [R] How to differ stats_cor labels by group on a ggplot

Hi Paulina,
Thanks for the data. Even after downloading a ton of packages along
with the "ggpubr" package, I get a namespace error when I try to load
it. After a bit of wrangling, I did get the code to run without the
"stat_cor" function, producing the four PNG images. My best guess is
to try the geom_label function with stat="cor", but I can't see any
argument to format the actual label. I think this is about as far as
I'm going to get.

Jim

On Sun, Jul 12, 2020 at 12:09 AM Paulina Skolasinska
<paulina.skol using hotmail.com> wrote:
>
> Thanks Tim, here is the link to the data: https://we.tl/t-c4x9Lw7LeR
> I'm posting my code again so it better matches the modified data, and because I've added several improvements in the meantime.
>
> for (i in 3:6) {
>     p1 <- ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), color=factor(AgeGroup))) +
>         geom_smooth(method="lm") +
>         geom_point(size = 4) +
>         scale_x_continuous(breaks = seq(20,90, by=10)) +
>         scale_y_continuous(breaks = seq(0.1,0.5, by=0.1)) +
>         theme_classic() +
>         expand_limits(y = 0.5) +
>         ylab(names(df)[i]) +
>         theme(axis.text.x = element_text(face="bold", size=14),
>               axis.text.y = element_text(face="bold", size=14),
>               axis.title.x = element_text(size=14, face="bold"),
>               axis.title.y = element_text(size=14, face="bold"),
>               legend.title = element_text(color="black", size=12),
>               legend.text = element_text(colour="black", size = 14, face = "bold")) +
>         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])),
>                  method = "pearson", label.x.npc = c("center"), label.y.npc="top") +
>         stat_cor(aes(x = Age, y = unlist(df[i]),
>                  label = paste(sub("R",expression("R"[overall]),..r.label..), ..p.label.., sep = "~`,`~"),
>                  group=1, color="black"), method = "pearson",label.y=0.5, label.x.npc = c("center"),
>                  position = position_nudge(y = 0.015 * 0.5))
>
>     ggsave(p1, file=paste0("TestAge_", names(df)[i], ".png"), scale=1, width=16, height=10, units="cm")
> }
>
>
> ________________________________
> Od: Jim Lemon <drjimlemon using gmail.com>
> Wys�ane: sobota, 11 lipca 2020 04:09
> Do: Paulina Skolasinska <paulina.skol using hotmail.com>
> DW: r-help using r-project.org <r-help using r-project.org>
> Temat: Re: [R] How to differ stats_cor labels by group on a ggplot
>
> Hi Paulina,
> Without data it's hard to work out what you are doing. Even a small
> simulated data set would help to get answers.
>
> Jim
>
> On Fri, Jul 10, 2020 at 11:49 PM Paulina Skolasinska
> <paulina.skol using hotmail.com> wrote:
> >
> > '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 using 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list