如何更改 `ggpubr::stat_regline_equation` 文本?
How do I change the `ggpubr::stat_regline_equation` text?
是否可以更改 ggpubr::stat_regline_equation
的标签?我想将标签更改为 ŷ = -51 + 32cyl
,如果可能的话,使用换行符和括号中的标准错误。
library(ggplot2)
library(ggpubr)
ggplot(mtcars, aes(cyl, hp)) +
geom_smooth(method = "lm") +
stat_regline_equation()
#> `geom_smooth()` using formula 'y ~ x'
由 reprex package (v0.3.0)
于 2020-06-06 创建
library(ggplot2)
library(broom)
library(stringr)
library(dplyr, quietly = T)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
add_parenthesis <- function(vector){
out <- as.vector(paste0("(", vector, ")"))
out
}
coef_label <- function(df){
coef <-
broom::tidy(lm(hp ~ cyl, data = df))[,2] %>%
mutate(estimate = round(estimate, 2)) %>%
pull()
paste0("ŷ = ", str_c(coef, collapse = " + "), "cyl")
}
se_label <- function(df){
broom::tidy(lm(hp ~ cyl, data = df))[,3] %>%
mutate(std.error = round(std.error, 2)) %>%
pull() %>%
add_parenthesis() %>%
str_c(., collapse = " ")
}
eqn_label = coef_label(mtcars)
se = se_label(mtcars)
ggplot(mtcars, aes(cyl, hp)) +
geom_smooth(method = "lm") +
annotate(x = 5, y = 200, label = eqn_label, geom = "text") +
annotate(x = 5, y = 190, label = se, geom = "text")
#> `geom_smooth()` using formula 'y ~ x'
由 reprex package (v0.3.0)
于 2020-06-06 创建
是否可以更改 ggpubr::stat_regline_equation
的标签?我想将标签更改为 ŷ = -51 + 32cyl
,如果可能的话,使用换行符和括号中的标准错误。
library(ggplot2)
library(ggpubr)
ggplot(mtcars, aes(cyl, hp)) +
geom_smooth(method = "lm") +
stat_regline_equation()
#> `geom_smooth()` using formula 'y ~ x'
由 reprex package (v0.3.0)
于 2020-06-06 创建library(ggplot2)
library(broom)
library(stringr)
library(dplyr, quietly = T)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
add_parenthesis <- function(vector){
out <- as.vector(paste0("(", vector, ")"))
out
}
coef_label <- function(df){
coef <-
broom::tidy(lm(hp ~ cyl, data = df))[,2] %>%
mutate(estimate = round(estimate, 2)) %>%
pull()
paste0("ŷ = ", str_c(coef, collapse = " + "), "cyl")
}
se_label <- function(df){
broom::tidy(lm(hp ~ cyl, data = df))[,3] %>%
mutate(std.error = round(std.error, 2)) %>%
pull() %>%
add_parenthesis() %>%
str_c(., collapse = " ")
}
eqn_label = coef_label(mtcars)
se = se_label(mtcars)
ggplot(mtcars, aes(cyl, hp)) +
geom_smooth(method = "lm") +
annotate(x = 5, y = 200, label = eqn_label, geom = "text") +
annotate(x = 5, y = 190, label = se, geom = "text")
#> `geom_smooth()` using formula 'y ~ x'
由 reprex package (v0.3.0)
于 2020-06-06 创建