如何在R中为ggplot的每个面添加R2?

2w3kk1z5  于 2023-01-28  发布在  其他
关注(0)|答案(3)|浏览(288)

有没有办法先将facet标签从1:3更改为类似c(good, bad, ugly)的内容。另外,我想为每个facet添加R2值。下面是我的代码-我尝试了一些方法,但没有成功。

DF = data.frame(SUB = rep(1:3, each = 100), Ob = runif(300, 50,100), S1 = runif(300, 75,95), S2 = runif(300, 40,90),
                S3 = runif(300, 35,80),S4 = runif(300, 55,100))
FakeData = gather(DF, key = "Variable", value = "Value", -c(SUB,Ob))

ggplot(FakeData, aes(x = Ob, y = Value))+
  geom_point()+ geom_smooth(method="lm") + facet_grid(Variable ~ SUB,  scales = "free_y")+
  theme_bw()

这是我使用上面的代码得到的图。x1c 0d1x我尝试了下面的代码来更改facet_label,但是没有成功

ggplot(FakeData, SUB = factor(SUB, levels = c("Good", "Bad","Ugly")), aes(x = Ob, y = Value))+
  geom_point()+ geom_smooth(method="lm") + facet_grid(Variable ~ SUB,  scales = "free_y")+
  theme_bw()

我不知道如何将R2添加到facets。有没有有效的方法将R2添加到facets

p4tfgftt

p4tfgftt1#

您可以使用ggpubr::stat_cor()轻松地将相关系数添加到图中。

library(dplyr)
library(ggplot2)
library(ggpubr)

FakeData %>%
  mutate(SUB = factor(SUB, labels = c("good", "bad", "ugly"))) %>%
  ggplot(aes(x = Ob, y = Value)) +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_grid(Variable ~ SUB,  scales = "free_y") +
  theme_bw() +
  stat_cor(aes(label = after_stat(rr.label)), color = "red", geom = "label")

mpbci0fu

mpbci0fu2#

如果您不想使用其他软件包中的函数,而只想使用ggplot2,您需要计算每个SUBVariable组合的R2,然后将geom_textgeom_label添加到绘图中。

library(tidyverse)

set.seed(1)

DF = data.frame(SUB = rep(1:3, each = 100), Ob = runif(300, 50,100), S1 = runif(300, 75,95), S2 = runif(300, 40,90),
                S3 = runif(300, 35,80),S4 = runif(300, 55,100))
FakeData = gather(DF, key = "Variable", value = "Value", -c(SUB,Ob))

FakeData_lm <- FakeData %>%
  group_by(SUB, Variable) %>%
  nest() %>%
  # Fit linear model
  mutate(Mod = map(data, ~lm(Value ~ Ob, data = .x))) %>%
  # Get the R2
  mutate(R2 = map_dbl(Mod, ~round(summary(.x)$r.squared, 3))) 

ggplot(FakeData, aes(x = Ob, y = Value))+
  geom_point()+ 
  geom_smooth(method="lm") + 
  # Add label
  geom_label(data = FakeData_lm, 
             aes(x = Inf, y = Inf, 
                 label = paste("R2 = ", R2, sep = " ")),
             hjust = 1, vjust = 1) +
  facet_grid(Variable ~ SUB,  scales = "free_y") +
  theme_bw()

ecbunoof

ecbunoof3#

下面的答案使用了包'ggpmisc'(第二个例子的版本〉= 0.5.0),另外,我只是在aes()中使用了对factor()的调用来设置标签。

library(tidyverse)
library(ggpmisc)

DF = data.frame(SUB = rep(1:3, each = 100), Ob = runif(300, 50,100), S1 = runif(300, 75,95), S2 = runif(300, 40,90),
                S3 = runif(300, 35,80),S4 = runif(300, 55,100))
FakeData = gather(DF, key = "Variable", value = "Value", -c(SUB,Ob))

# As asked in the question
# Ensuring that the R^2 label does not overlap the observations
ggplot(FakeData, aes(x = Ob, y = Value)) +
  geom_point()+ 
  geom_smooth(method = "lm") + 
  stat_poly_eq() +
  scale_y_continuous(expand = expansion(mult = c(0.1, 0.33))) +
  facet_grid(Variable ~ factor(SUB, 
                               levels = 1:3,
                               labels = c("good", "bad", "ugly")), 
             scales = "free_y") +
  theme_bw()

# As asked in a comment, adding P-value
ggplot(FakeData, aes(x = Ob, y = Value))+
  geom_point()+ 
  geom_smooth(method = "lm") + 
  stat_poly_eq(mapping = use_label(c("R2", "P")), p.digits = 2) +
  scale_y_continuous(expand = expansion(mult = c(0.1, 0.33))) +
  facet_grid(Variable ~ factor(SUB, 
                               levels = 1:3,
                               labels = c("good", "bad", "ugly")),
             scales = "free_y")+
  theme_bw()

以及第二个示例中的图,将 P 添加到标签。

  • 注意:* 对于缺少use_label()函数的旧版本'ggpmisc',Map可以写成aes(label = paste(after_stat(rr.label), after_stat(p.label), sep = "*\", \"*"),方法与使用'ggpubr'相同。

包'ggpubr'包含从'ggpmisc'复制的代码,但没有确认,这解释了为什么这两个包之间的某些统计信息如此相似。'ggpmisc'关注的范围更窄,但其中的统计信息在被纳入'ggpubr'后得到了很大改善。'ggpmisc'正在积极维护,而'ggpubr'的维护目前似乎停止了。

相关问题