如何将回归线的R平方值添加到ggplot2中的每个面板中?

huangapple go评论63阅读模式
英文:

How to add r squared from regression lines in geom_smooth to each facet in ggplot2?

问题

我已经在R中有以下数据:

data <- structure(list(Date = structure(c(662688000, 694224000, 725846400, 
757382400, 788918400, 820454400, 852076800, 883612800, 915148800, 
946684800, 978307200, 1009843200, 1041379200, 1072915200, 1104537600, 
1136073600, 1167609600, 1199145600, 1230768000, 1262304000, 1293840000, 
1325376000, 1356998400, 1388534400, 1420070400, 1451606400, 1483228800, 
1514764800, 1546300800, 1577836800, 1609459200, 1640995200, 662688000, 
694224000, 725846400, 757382400, 788918400, 820454400, 852076800, 
883612800, 915148800, 946684800, 978307200, 1009843200, 1041379200, 
1072915200, 1104537600, 1136073600, 1167609600, 1199145600, 1230768000, 
1262304000, 1293840000, 1325376000, 1356998400, 1388534400, 1420070400, 
1451606400, 1483228800, 1514764800, 1546300800, 1577836800, 1609459200, 
1640995200, 662688000, 694224000, 725846400, 757382400, 788918400, 
820454400, 852076800, 883612800, 915148800, 946684800, 978307200, 
1009843200, 1041379200, 1072915200, 1104537600, 1136073600, 1167609600, 
1199145600, 1230768000, 1262304000, 1293840000, 1325376000, 1356998400, 
1388534400, 1420070400, 1451606400, 1483228800, 1514764800, 1546300800, 
1577836800, 1609459200, 1640995200, 662688000, 694224000, 725846400, 
757382400, 788918400, 820454400, 852076800, 883612800, 915148800, 
946684800, 978307200, 1009843200, 1041379200, 1072915200, 1104537600, 
1136073600, 1167609600, 1199145600, 1230768000, 1262304000, 1293840000, 
1325376000, 1356998400, 1388534400, 1420070400, 1451606400, 1483228800, 
1514764800, 1546300800, 1577836800, 1609459200, 1640995200, 662688000, 
694224000, 725846400, 757382400, 788918400, 820454400, 852076800, 
883612800, 915148800, 946684800, 978307200, 1009843200, 1041379200, 
1072915200, 1104537600, 1136073600, 1167609600, 1199145600, 1230768000, 
1262304000, 1293840000, 1325376000, 1356998400, 1388534400, 1420070400, 
1451606400, 1483228800, 1514764800, 1546300800, 1577836800, 1609459200, 
1640995200), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), ZAR_USD = c(-6.72487789009559, -3.28463672803248, 
-14.5766252329857, -8.66215221679989, -2.14843704162313, -18.5345578244422, 
-7.17812133968339, -19.9724426237631, -10.5132077599122, -13.5910683130503, 
-24.0546656173292, -22.4361164055779, 28.2332703186114, 14.6079749945444, 
1.55369882802734, -6.48214420967423, -4.04362172663832, -17.2575634240856, 
-2.57166301337393, 13.6003836774172, 0.82076228728446, -13.0673354646948, 
-17.6016195465241, -12.4038585697732, -17.5650586190588, -15.2887418431118, 
9.42111570327837, 0.674544374030084, -9.17717493638679, -13.9162438088884, 
10.2097115035398, -10.6719643513052, -6.72487789009559, -3.28463672803248, 
-14.5766252329857, -8.66215221679989, -2.14843704162313, -18.5345578244422, 
-7.17812133968339, -19.9724426237631, -10.5132077599122, -13.5910683130503, 
-24.0546656173292, -22.4361164055779, 28.2332703186114, 14.6079749945444, 
1.55369882802734, -6.48214420967423, -4.04362172663832, -17.2575634240856, 
-2.57166301337393, 13.6003836774172, 0.82076228728446, -13.0673354646948, 
-17.6016195465241, -12.4038585697732, -17.5650586190588, -15.2887418431118, 
9.42111570327837, 0.674544374030084, -9.17717493638679, -13.9162438088884, 
10.209711

<details>
<summary>英文:</summary>

I have the following data in R

data <- structure(list(Date = structure(c(662688000, 694224000, 725846400,
757382400, 788918400, 820454400, 852076800, 883612800, 915148800,
946684800, 978307200, 1009843200, 1041379200, 1072915200, 1104537600,
1136073600, 1167609600, 1199145600, 1230768000, 1262304000, 1293840000,
1325376000, 1356998400, 1388534400, 1420070400, 1451606400, 1483228800,
1514764800, 1546300800, 1577836800, 1609459200, 1640995200, 662688000,
694224000, 725846400, 757382400, 788918400, 820454400, 852076800,
883612800, 915148800, 946684800, 978307200, 1009843200, 1041379200,
1072915200, 1104537600, 1136073600, 1167609600, 1199145600, 1230768000,
1262304000, 1293840000, 1325376000, 1356998400, 1388534400, 1420070400,
1451606400, 1483228800, 1514764800, 1546300800, 1577836800, 1609459200,
1640995200, 662688000, 694224000, 725846400, 757382400, 788918400,
820454400, 852076800, 883612800, 915148800, 946684800, 978307200,
1009843200, 1041379200, 1072915200, 1104537600, 1136073600, 1167609600,
1199145600, 1230768000, 1262304000, 1293840000, 1325376000, 1356998400,
1388534400, 1420070400, 1451606400, 1483228800, 1514764800, 1546300800,
1577836800, 1609459200, 1640995200, 662688000, 694224000, 725846400,
757382400, 788918400, 820454400, 852076800, 883612800, 915148800,
946684800, 978307200, 1009843200, 1041379200, 1072915200, 1104537600,
1136073600, 1167609600, 1199145600, 1230768000, 1262304000, 1293840000,
1325376000, 1356998400, 1388534400, 1420070400, 1451606400, 1483228800,
1514764800, 1546300800, 1577836800, 1609459200, 1640995200), tzone = "UTC", class = c("POSIXct",
"POSIXt")), ZAR_USD = c(-6.72487789009559, -3.28463672803248,
-14.5766252329857, -8.66215221679989, -2.14843704162313, -18.5345578244422,
-7.17812133968339, -19.9724426237631, -10.5132077599122, -13.5910683130503,
-24.0546656173292, -22.4361164055779, 28.2332703186114, 14.6079749945444,
1.55369882802734, -6.48214420967423, -4.04362172663832, -17.2575634240856,
-2.57166301337393, 13.6003836774172, 0.82076228728446, -13.0673354646948,
-17.6016195465241, -12.4038585697732, -17.5650586190588, -15.2887418431118,
9.42111570327837, 0.674544374030084, -9.17717493638679, -13.9162438088884,
10.2097115035398, -10.6719643513052, -6.72487789009559, -3.28463672803248,
-14.5766252329857, -8.66215221679989, -2.14843704162313, -18.5345578244422,
-7.17812133968339, -19.9724426237631, -10.5132077599122, -13.5910683130503,
-24.0546656173292, -22.4361164055779, 28.2332703186114, 14.6079749945444,
1.55369882802734, -6.48214420967423, -4.04362172663832, -17.2575634240856,
-2.57166301337393, 13.6003836774172, 0.82076228728446, -13.0673354646948,
-17.6016195465241, -12.4038585697732, -17.5650586190588, -15.2887418431118,
9.42111570327837, 0.674544374030084, -9.17717493638679, -13.9162438088884,
10.2097115035398, -10.6719643513052, -6.72487789009559, -3.28463672803248,
-14.5766252329857, -8.66215221679989, -2.14843704162313, -18.5345578244422,
-7.17812133968339, -19.9724426237631, -10.5132077599122, -13.5910683130503,
-24.0546656173292, -22.4361164055779, 28.2332703186114, 14.6079749945444,
1.55369882802734, -6.48214420967423, -4.04362172663832, -17.2575634240856,
-2.57166301337393, 13.6003836774172, 0.82076228728446, -13.0673354646948,
-17.6016195465241, -12.4038585697732, -17.5650586190588, -15.2887418431118,
9.42111570327837, 0.674544374030084, -9.17717493638679, -13.9162438088884,
10.2097115035398, -10.6719643513052, -6.72487789009559, -3.28463672803248,
-14.5766252329857, -8.66215221679989, -2.14843704162313, -18.5345578244422,
-7.17812133968339, -19.9724426237631, -10.5132077599122, -13.5910683130503,
-24.0546656173292, -22.4361164055779, 28.2332703186114, 14.6079749945444,
1.55369882802734, -6.48214420967423, -4.04362172663832, -17.2575634240856,
-2.57166301337393, 13.6003836774172, 0.82076228728446, -13.0673354646948,
-17.6016195465241, -12.4038585697732, -17.5650586190588, -15.2887418431118,
9.42111570327837, 0.674544374030084, -9.17717493638679, -13.9162438088884,
10.2097115035398, -10.6719643513052), Key = c("GDP_NAM", "GDP_NAM",
"GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM",
"GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM",
"GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM",
"GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM",
"GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM", "GDP_NAM",
"GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA",
"GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA",
"GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA",
"GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA",
"GDP_SA", "GDP_SA", "GDP_SA", "GDP_SA", "GDP_ESW", "GDP_ESW",
"GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW",
"GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW",
"GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW",
"GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW",
"GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW", "GDP_ESW",
"GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES",
"GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES",
"GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES",
"GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES",
"GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES", "GDP_LES",
"GDP_LES", "GDP_LES"), GDP = c(8.16561250262779, 7.18934252280525,
-1.57953915916903, 1.72987948847074, 3.8990142211043, 3.19132430111468,
4.22010030384476, 3.29158562064817, 3.36927842445498, 3.49218336365178,
1.17794869467918, 4.78866121882111, 4.23979429582138, 12.2695481036769,
2.52926265819022, 7.07317531845826, 5.37404415167635, 2.64981203780268,
0.295970914001087, 6.03924947955858, 5.09133813864338, 5.06168206962641,
5.61471962254485, 6.09251915977955, 4.26417453763674, 0.0337944502063259,
-1.02725082701774, 1.05994347072929, -0.839154686169741, -8.1014007802753,
3.52465798278794, 4.56029396793913, -1.0182449675445, -2.1370328450052,
1.23355793864741, 3.20000000297171, 3.10000000054411, 4.29999999726791,
2.60000000146348, 0.500000000341427, 2.39999999746339, 4.20000000067826,
2.70000000019104, 3.70037440406668, 2.94907546754213, 4.55455990721774,
5.27705197295467, 5.60380645895888, 5.36047405394162, 3.1910438863288,
-1.53808913525584, 3.03973288127957, 3.16855627858819, 2.39623238465745,
2.48546800826588, 1.41382645223793, 1.3218622367823, 0.664552307858113,
1.15794695181735, 1.5223293868265, 0.303453235238083, -6.34247114929317,
4.91309668391779, 2.04229935251898, 1.76037634931168, 3.22620688108395,
3.1061141567694, 2.40069103902882, 4.8256515499647, 3.84205363771881,
3.10280535039624, 2.60403940132949, 2.9508752114854, 1.76017376022224,
1.05476490606893, 4.38009908790089, 3.88021358597126, 3.62391422596624,
5.99877654987926, 5.9921668378351, 4.43537557686975, 0.821663913800053,
1.56504885243516, 3.79375496828149, 2.24722976106799, 5.39663069666727,
3.86121379766622, 0.92323054714436, 2.22748067532406, 1.06261019342853,
2.0265781557498, 2.38009547557818, 2.69216829254753, -1.55964258999499,
7.88330640157318, 3.91454097653912, 6.96660711388259, 6.9563649569206,
3.50976647766819, 5.97987312755175, 3.29093854641436, 5.59703575933248,
3.70360359587891, 1.53765563950929, 0.476025102065125, 3.87554681425579,
3.56158219085148, 0.723835961487329, 4.55977681130346, 1.69237420642219,
3.46612248144242, 4.23009485409367, 4.18791441799631, 5.52202820126872,
-1.25551462572604, 5.26628065286225, 4.61425223766248, 6.33482170116899,
1.79253144466578, 1.71054400422559, 3.12755772382955, 3.60540004794543,
-3.13817492339221, -1.48063361037254, -0.764225981347111, -5.61940242631855,
1.55767983330564, 0.591917768214745)), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -128L))


I&#39;ve created a grid of scatter plots (with regression lines) with my data using ```ggplot2```. Is there a simple way to add the R Squared of each regression line from ```geom_smooth``` to each facet in my plot?

library(tidyverse)

data %>% {
ggplot(., aes(x = ZAR_USD, y = GDP)) +
geom_point(size = 2) +
geom_smooth(method="lm", se=TRUE) +
facet_wrap(~Key, scales = 'free')
}


</details>
# 答案1
**得分**: 1
你可以使用 `{ggpmisc}` 包中的 `stat_poly_eq()` 函数来实现这个功能:
```r
library(ggpmisc)
library(tidyverse)
data %>%
ggplot(aes(x = ZAR_USD, y = GDP)) +
geom_point(size = 2) +
geom_smooth(method="lm", se=TRUE) +
stat_poly_eq(mapping = use_label("R2")) +
facet_wrap(~Key, scales = 'free')

这将得到以下结果:

如何将回归线的R平方值添加到ggplot2中的每个面板中?

更多示例:https://cran.rstudio.com/web/packages/ggpmisc/vignettes/model-based-annotations.html#stat_poly_eq-and-stat_poly_line

英文:

You can do this using stat_poly_eq() from the {ggpmisc} package:

library(ggpmisc)
library(tidyverse)
data %&gt;% 
  ggplot(aes(x = ZAR_USD, y = GDP)) +
    geom_point(size = 2) +
    geom_smooth(method=&quot;lm&quot;, se=TRUE) +
    stat_poly_eq(mapping = use_label(&quot;R2&quot;)) +
    facet_wrap(~Key, scales = &#39;free&#39;)

which gives:

如何将回归线的R平方值添加到ggplot2中的每个面板中?

Further examples: https://cran.rstudio.com/web/packages/ggpmisc/vignettes/model-based-annotations.html#stat_poly_eq-and-stat_poly_line

huangapple
  • 本文由 发表于 2023年7月20日 19:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定