How to differ stats_cor labels by group on a ggplot

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to differ stats_cor labels by group on a ggplot

Paulina Skolasinska
'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)
}
______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to differ stats_cor labels by group on a ggplot

Jim Lemon-4
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
<[hidden email]> 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)
> }
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

ODP: How to differ stats_cor labels by group on a ggplot

Paulina Skolasinska
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 <[hidden email]>
Wys�ane: sobota, 11 lipca 2020 04:09
Do: Paulina Skolasinska <[hidden email]>
DW: [hidden email] <[hidden email]>
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
<[hidden email]> 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)
> }
> ______________________________________________
> [hidden email] 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]]


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to differ stats_cor labels by group on a ggplot

Jim Lemon-4
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
<[hidden email]> 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 <[hidden email]>
> Wysłane: sobota, 11 lipca 2020 04:09
> Do: Paulina Skolasinska <[hidden email]>
> DW: [hidden email] <[hidden email]>
> 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
> <[hidden email]> 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)
> > }
> > ______________________________________________
> > [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to differ stats_cor labels by group on a ggplot

Paulina Skolasinska
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 <[hidden email]>
Sent: Saturday, July 11, 2020 6:32:13 PM
To: Paulina Skolasinska <[hidden email]>
Cc: [hidden email] <[hidden email]>
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
<[hidden email]> 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 <[hidden email]>
> Wys�ane: sobota, 11 lipca 2020 04:09
> Do: Paulina Skolasinska <[hidden email]>
> DW: [hidden email] <[hidden email]>
> 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
> <[hidden email]> 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)
> > }
> > ______________________________________________
> > [hidden email] 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]]


______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to differ stats_cor labels by group on a ggplot

Jim Lemon-4
Okay. If you get stuck, you can always roll your own:

psdf<-read.table("sim_data.txt",header=TRUE,stringsAsFactors=FALSE)
library(plotrix)
source("supsubtext.R")
par(cex.axis=1.5)
for(i in 3:6) {
 png(paste0("TestAge_",names(psdf[i]),".png"),width=600)
 plot(psdf$Age,psdf[,i],xlim=c(20,85),ylim=c(-0.1,0.5),
  xlab="Age",ylab=names(psdf)[i],
  axes=FALSE,pch=19,col=c("green","orange")[psdf$AgeGroup+1],
  cex=2)
 fullaxis(1,col="black",lwd=1.5)
 fullaxis(2,col="black",lwd=1.5)
 # fit the entire column
 lmfit<-lm(psdf[,i]~psdf$Age)
 lmpf<-predict(lmfit,se.fit=TRUE)
 uniqage<-sort(unique(psdf$Age))
 cis<-supsmu(psdf$Age,lmpf$se.fit)$y
 lmline<-lmfit$coefficients[1]+uniqage*lmfit$coefficients[2]
 # display the overall confidence band
 polygon(c(uniqage,rev(uniqage)),c(lmline+cis,rev(lmline-cis)),
  border=NA,col="lightgray")
 # now the "young" group
 young<-psdf$AgeGroup == 0
 lmfit<-lm(psdf[young,i]~psdf[young,"Age"])
 lmpf<-predict(lmfit,se.fit=TRUE)
 uniqage<-sort(unique(psdf$Age[young]))
 cis<-supsmu(psdf$Age[young],lmpf$se.fit)$y
 lmline<-lmfit$coefficients[1]+uniqage*lmfit$coefficients[2]
 polygon(c(uniqage,rev(uniqage)),c(lmline+cis,rev(lmline-cis)),
  border=NA,col="#88ff8880")
 # now the "old" group
 old<-psdf$AgeGroup == 1
 lmfit<-lm(psdf[old,i]~psdf[old,"Age"])
 lmpf<-predict(lmfit,se.fit=TRUE)
 uniqage<-sort(unique(psdf$Age[old]))
 cis<-supsmu(psdf$Age[old],lmpf$se.fit)$y
 lmline<-lmfit$coefficients[1]+uniqage*lmfit$coefficients[2]
 polygon(c(uniqage,rev(uniqage)),c(lmline+cis,rev(lmline-cis)),
  border=NA,col="#ffaa8880")
 points(psdf$Age,psdf[,i],pch=19,
  col=c("green","orange")[psdf$AgeGroup+1],cex=2)
 # overall correlation
 corval<-cor.test(psdf$Age,psdf[,i])
 corstr<-paste0("R(overall) = ",round(corval$estimate,2),", p = ",
  round(corval$p.value,2))
 supsubtext(20,0.05,corstr,sub=2:10,col="blue")
 # young correlation
 corval<-cor.test(psdf[young,"Age"],psdf[young,i])
 corstr<-paste0("R(young) = ",round(corval$estimate,2),", p = ",
  round(corval$p.value,2))
 supsubtext(20,0,corstr,sub=2:8,col="green")
 # old correlation
 corval<-cor.test(psdf[old,"Age"],psdf[old,i])
 corstr<-paste0("R(old) = ",round(corval$estimate,2),", p = ",
  round(corval$p.value,2))
 supsubtext(20,-0.05,corstr,sub=2:6,col="orange")
 legend(40,0.5,c("Overall","Young","Old"),pch=19,
  col=c("lightgray","green","orange"))
 dev.off()
}

I've been meaning to finish off the "supsubtext" function for a while.

Jim

On Sun, Jul 12, 2020 at 11:20 AM Paulina Skolasinska
<[hidden email]> wrote:

>
> 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
> ________________________________
> From: Jim Lemon <[hidden email]>
> Sent: Saturday, July 11, 2020 6:32:13 PM
> To: Paulina Skolasinska <[hidden email]>
> Cc: [hidden email] <[hidden email]>
> 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
> <[hidden email]> 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 <[hidden email]>
> > Wysłane: sobota, 11 lipca 2020 04:09
> > Do: Paulina Skolasinska <[hidden email]>
> > DW: [hidden email] <[hidden email]>
> > 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
> > <[hidden email]> 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)
> > > }
> > > ______________________________________________
> > > [hidden email] 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.
______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to differ stats_cor labels by group on a ggplot

Rui Barradas
In reply to this post by Paulina Skolasinska
Hello,

The code below puts the group names as subscripts to 'R'.
The trick is to create each of the labels with those names before
pasting the ..p.label..
This is done by Map, calling a function f defined outside the for loop.

I have also changed your code a bit, simplifying parts of it.

1. I have named the data set df1, since df already is a base R function.
2. Instead of redefining theme() elements in the loop, I have created a
custom one.
3. The for loop runs directly through the names of the data set df1, not
through indices into them.
4. df1[[i]] is the same as unlist(df1[i]). And simpler.
5. After testing the code with df1[[i]], there were warnings, telling to
use the pronoun .data, which I do.
6. The code repeated itself a lot, if you define aesthetics in the
initial call to ggplot, there is no need to redefine them in the layers
calls unless you are changing them. For instance, after

ggplot(df, mapping=aes(x = Age, y = unlist(df[i]), color=factor(AgeGroup)))


you don't have to define the same y and color again:

stat_cor(aes(color = factor(AgeGroup), y = unlist(df[i]))


7. Some arguments (method = "pearson" and others) didn't change their
default values and were removed.


Now the code.
Note that before writing to file, the filename is output to stdin,
remove it, it's not part of your original code.



theme_custom_ps <- function(){

   theme_classic() %+replace%    # replace elements we want to change

     theme(axis.text.x = element_text(face = "bold", size = 14),
           axis.text.y = element_text(face = "bold", size = 14),
           axis.title.x = element_text(face = "bold", size = 14),
           axis.title.y = element_text(face = "bold", size = 14),
           legend.text = element_text(face = "bold", size = 14, colour =
"black"),
           legend.title = element_text(size = 12, colour = "black")
     )
}


library(ggplot2)
library(ggpubr)

df1 <- read.table("sim_data.txt", header = TRUE)

group_name <- c("old", "young")
f <- function(grp, rlab) sub("R", paste0("R[", grp, "]"), rlab)

for (i in names(df1)[3:6]) {
   p1 <- ggplot(df1, mapping = aes(x = Age, y = .data[[i]], color =
factor(AgeGroup))) +
     geom_point(size = 4) +
     geom_smooth(method = "lm", formula = y ~ x) +
     geom_smooth(mapping = aes(group = 1), method = "lm", formula = y ~ x) +
     stat_cor(aes(label = paste(Map(f, group_name, ..r.label..),
..p.label.., sep = "~`,`~")),
              label.x.npc = "center",
              show.legend = FALSE) +
     stat_cor(aes(label = paste(sub("R", expression("R"[overall]),
..r.label..), ..p.label.., sep = "~`,`~"),
                  group = 1, color = "black"),
              label.y = 0.5,
              label.x.npc = "center",
              position = position_nudge(y = 0.015 * 0.5),
              show.legend = FALSE) +
     scale_colour_discrete(name = "Group", labels = c("Young", "Old",
"Overall")) +
     scale_x_continuous(breaks = seq(20, 90, by = 10)) +
     scale_y_continuous(breaks = seq(0.1, 0.5, by = 0.1)) +
     expand_limits(y = 0.5) +
     ylab(i) +
     theme_custom_ps()

   pngfile <- paste0("TestAge_", i, ".png")
   cat("filename:", pngfile, "\n")
   ggsave(p1, file = pngfile, scale = 1, width = 16, height = 10, units
= "cm")
}


So it seems this is worth the effort after all.
Just trickier than expected.


Hope this helps,

Rui Barradas

Às 02:20 de 12/07/20, Paulina Skolasinska escreveu:

> 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 <[hidden email]>
> Sent: Saturday, July 11, 2020 6:32:13 PM
> To: Paulina Skolasinska <[hidden email]>
> Cc: [hidden email] <[hidden email]>
> 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
> <[hidden email]> 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 <[hidden email]>
>> Wys�ane: sobota, 11 lipca 2020 04:09
>> Do: Paulina Skolasinska <[hidden email]>
>> DW: [hidden email] <[hidden email]>
>> 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
>> <[hidden email]> 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)
>>> }
>>> ______________________________________________
>>> [hidden email] 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]]
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.