在 stat_cor 结果上粘贴分组变量的标签

问题描述 投票:0回答:0

我有这种情况:

library(ggplot2)
library(ggpubr)

# Load data
data("mtcars")
df <- mtcars
df$vs <- factor(df$vs, labels = c("Alpha", "Beta"))
df$MyFactor <- rep(c("White", "Black"), nrow(df)/2)


# Plotting
ggplot(df, aes(x=wt, y=mpg, group = vs))+
  geom_point()+
  geom_smooth(method = "lm")+
  facet_wrap(~MyFactor)+
  stat_cor(method = "pearson", label.y.npc="top", label.x.npc = "left", color = "black")+
  theme_bw()

输出:

我想在

paste
标签上
stat_cor
相对分组变量
vs
(即
Alpha
Beta
)。因此,我使用了:

ggplot(df, aes(x=wt, y=mpg, group = vs))+
  geom_point()+
  geom_smooth(method = "lm")+
  facet_wrap(~MyFactor)+
  stat_cor(aes(label = paste(..group.., "- ", ..r.label.., ..p.label.., sep = "~`,`~")), method = "pearson", label.y.npc="top", label.x.npc = "left", color = "black")+
  theme_bw()

然而,我的输出是:

如何获取我的 vs 分组标签?

r ggplot2 paste ggpubr
© www.soinside.com 2019 - 2024. All rights reserved.