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

9

有没有一种方法可以先将facets标签从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()

这是使用上述代码得到的图表。 enter image description here 我尝试了下面的代码来更改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中?


要将SUB级别更改为“good”,“bad”和“ugly”,您可以使用fct_recode()case_when(),例如:DF%>% mutate(SUB = fct_recode(as.character(SUB),good =“1”,bad =“2”,ugly =“3”))"DF%>% mutate(SUB = case_when(SUB == 1〜“好”的1,SUB == 2〜“坏”的2,SUB == 3〜“丑”的3)) - Adam B.
要获得R2,您可能需要在您想要R2的数据子集上迭代线性模型。最简单和无痛苦的方法是使用嵌套数据框架(请参见:https://r4ds.had.co.nz/many-models.html)。尝试阅读链接并找出自己的方法-学习如何正确迭代我的工作流程对我来说是一项宝贵的技能。如果您遇到困难,请在此处发表评论或创建新帖子。 - Adam B.
要更改 SUB,请使用 labels 而不是 levels,像这样 mutate(SUB = factor(SUB, labels = c("好", "坏", "丑"))) - G. Grothendieck
3个回答

18
您可以使用 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")

输入图片描述


有没有办法在图形上同时显示'R ^ 2'和'p值'? - Hydro
2
使用 stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~")), color = "red", geom = "label") - Ritchie Sacramento
哇,这个工作得像魔法一样。在深夜的紧急模式下,没有任何问题。几乎太好了,让人难以置信! - Anson Call
1
在ggplot2 3.4.0中,首选的语法已更改,现在是aes(label = after_stat(rr.label)) - G. Grothendieck
@G.Grothendieck - 谢谢 Gabor - 已更新。 - Ritchie Sacramento

5

如果您不想使用其他包中的函数,只想使用 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()

enter image description here


1
以下答案使用了包 '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()

enter image description here

第二个例子中的图表添加了标签P

注:使用较旧版本的“ggpmisc”时,缺少函数use_label(),则可以将映射写成aes(label = paste(after_stat(rr.label), after_stat(p.label), sep = "*\", \"*"),与使用“ggpubr”时相同。

enter image description here

软件包“ggpubr”未经确认地复制了来自“ggpmisc”的代码,这解释了为什么两个软件包之间的一些统计数据如此相似。“ggpmisc”更加专注于某些方面,但其中的统计数据在被纳入“ggpubr”之后得到了大幅改进。“ggpmisc”正在积极维护,而“ggpubr”的维护目前似乎已经停滞不前。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接