如何在 R 函数中创建一个我可以在函数中使用的新对象
How do I create a new object in an R function, that I can use within the function
我正在尝试编写一个函数来创建一个 ggplot,其组件全部基于单个输入。我有一个数据框,包含种族、三项评估(阅读、写作、数学)的结果以及每个结果的置信区间。我想创建一个函数,该函数将根据读取、写入或数学创建相同的绘图。
例如,如果我使用 var Read_perc
library(tidyverse)
dat <- data.frame (Race = c("White", "Black", "Inidigenous"),
Read_perc = c (0.756, 0.592, 0.548),
Read_low_ci = c (0.742, 0.498, 0.467),
Read_high_ci = c(0.769, 0.679, 0.628),
Write_perc = c (0.717, 0.625, 0.497),
Write_low_ci = c (0.703, 0.532, 0.416),
Write_high_ci = c (0.731, 0.710, 0.578))
my_plot_function <- function (df, var){
var <- enquo(var)
df %>%
ggplot (aes (x = reorder (Race, !! var), y = !! var)) +
geom_col () +
coord_flip () +
geom_text (aes (label = scales::percent(!! var, accuracy = 1)),
y = 0.05,
colour = "white")
}
my_plot_function(dat, Read_perc)
但是,我只想使用 Read 或 Write,然后用它来调用不同的列,例如 Read_perc、Read_low_ci 等
例如,这行不通:
my_plot_function <- function (df, var){
var <- enquo (var)
var_low_ci <- paste0 (var, "_low_ci")
var_high_ci <- paste0 (var, "_high_ci")
df %>%
ggplot (aes (x = reorder (Race, !! var), y = !! var)) +
geom_col () +
coord_flip () +
geom_text (aes (label = scales::percent(!! var, accuracy = 1)),
y = 0.05,
colour = "white") +
geom_errorbar(aes(ymin = var_low_ci,
ymax = var_high_ci),
colour = "black")
最终图表应如下所示:
我们可以转换为 sym
bol 求值
my_plot_function <- function (df, var){
var <- rlang::as_string(rlang::ensym(var))
var_low_ci <- rlang::sym(str_c(str_remove(var, "_perc"), "_low_ci"))
var_high_ci <- rlang::sym(str_c(str_remove(var, "_perc"), "_high_ci"))
var1 <- rlang::sym(var)
df %>%
ggplot(aes(x = reorder(Race, !! var1), y = !! var1)) +
geom_col () +
coord_flip () +
geom_text(aes(label = scales::percent(!! var1, accuracy = 1)),
y = 0.05,
colour = "white") +
geom_errorbar(aes(ymin = !!var_low_ci,
ymax = !!var_high_ci),
colour = "black")
}
-测试
my_plot_function(dat, Read_perc)
我正在尝试编写一个函数来创建一个 ggplot,其组件全部基于单个输入。我有一个数据框,包含种族、三项评估(阅读、写作、数学)的结果以及每个结果的置信区间。我想创建一个函数,该函数将根据读取、写入或数学创建相同的绘图。
例如,如果我使用 var Read_perc
library(tidyverse)
dat <- data.frame (Race = c("White", "Black", "Inidigenous"),
Read_perc = c (0.756, 0.592, 0.548),
Read_low_ci = c (0.742, 0.498, 0.467),
Read_high_ci = c(0.769, 0.679, 0.628),
Write_perc = c (0.717, 0.625, 0.497),
Write_low_ci = c (0.703, 0.532, 0.416),
Write_high_ci = c (0.731, 0.710, 0.578))
my_plot_function <- function (df, var){
var <- enquo(var)
df %>%
ggplot (aes (x = reorder (Race, !! var), y = !! var)) +
geom_col () +
coord_flip () +
geom_text (aes (label = scales::percent(!! var, accuracy = 1)),
y = 0.05,
colour = "white")
}
my_plot_function(dat, Read_perc)
但是,我只想使用 Read 或 Write,然后用它来调用不同的列,例如 Read_perc、Read_low_ci 等
例如,这行不通:
my_plot_function <- function (df, var){
var <- enquo (var)
var_low_ci <- paste0 (var, "_low_ci")
var_high_ci <- paste0 (var, "_high_ci")
df %>%
ggplot (aes (x = reorder (Race, !! var), y = !! var)) +
geom_col () +
coord_flip () +
geom_text (aes (label = scales::percent(!! var, accuracy = 1)),
y = 0.05,
colour = "white") +
geom_errorbar(aes(ymin = var_low_ci,
ymax = var_high_ci),
colour = "black")
最终图表应如下所示:
我们可以转换为 sym
bol 求值
my_plot_function <- function (df, var){
var <- rlang::as_string(rlang::ensym(var))
var_low_ci <- rlang::sym(str_c(str_remove(var, "_perc"), "_low_ci"))
var_high_ci <- rlang::sym(str_c(str_remove(var, "_perc"), "_high_ci"))
var1 <- rlang::sym(var)
df %>%
ggplot(aes(x = reorder(Race, !! var1), y = !! var1)) +
geom_col () +
coord_flip () +
geom_text(aes(label = scales::percent(!! var1, accuracy = 1)),
y = 0.05,
colour = "white") +
geom_errorbar(aes(ymin = !!var_low_ci,
ymax = !!var_high_ci),
colour = "black")
}
-测试
my_plot_function(dat, Read_perc)