如何使用ggplot和dplyr在函数中创建因子变量? [英] how to create factor variables from quosures in functions using ggplot and dplyr?
问题描述
这是
理想情况下,我想定义因子变量。也就是说,在函数中有这样的东西:
ggplot(df_agg,aes_q(x = quote(count),
y = quote(mean),
color = factor(quo_var),
group = factor(quo_var)))+
geom_point()+
geom_line()
这当然不起作用。
问题是:这里可以做什么?
谢谢!!
以下是使用其他rlang函数的可能性。
get_charts1< - function(data,mygroup){
quo_var< - enquo(mygroup)
df_agg< - data%>%
group_by (!! quo_var)%>%
汇总(均值=均值(值,na.rm = TRUE),
count = n())%>%
ungroup()$ (!!(rlang :: get_expr(quo_var))))
#或者只是cc< - expr(factor(!! get_expr(如果包含库(rlang)
ggplot(df_agg,aes_q(x = quote(count),y = quote(mean),color = cc))+
geom_point ()+
geom_line()
}
c $ c> factor(group)使用 expr()
函数。我们使用 get_expr()
从静默 quo_var
中提取符号名称group。一旦我们构建了表达式,我们可以将它传递给 aes_q
。
希望ggplot很快就会被整理 - 适合消费者,这将不再是必要的。
This is a follow up from how to combine ggplot and dplyr into a function?.
The issue is, how to write a function that uses dplyr
, ggplot
and possibly specifying factor variables from quosures
?
Here is an example
dataframe <- data_frame(id = c(1,2,3,4,5,6),
group = c(1,1,0,0,3,4),
value = c(200,400,120,300,100,100))
# A tibble: 6 x 3
id group value
<dbl> <dbl> <dbl>
1 1 1 200
2 2 1 400
3 3 0 120
4 4 0 300
5 5 3 100
6 6 4 100
As you can see, the grouping variable group
is numeric here, so
get_charts1 <- function(data, mygroup, myoutput){
quo_var <- enquo(mygroup)
quo_output <- enquo(myoutput)
df_agg <- data %>%
group_by(!!quo_var) %>%
summarize(mean = mean(!!quo_output, na.rm = TRUE),
count = n()) %>%
ungroup()
ggplot(df_agg, aes_q(x = quote(count), y = quote(mean), color = quo_var, group = quo_var)) +
geom_point() +
geom_line()
}
get_charts1(dataframe,
mygroup = group,
myoutput = value)
will output a chart with a continuous scale for the grouping variable, which is not desired.
Ideally, I would like to define factor variables on-the-fly. That is, having something like this in the function:
ggplot(df_agg, aes_q(x = quote(count),
y = quote(mean),
color = factor(quo_var),
group = factor(quo_var))) +
geom_point() +
geom_line()
which of course does not work.
The question is: What can be done here?
Thanks!!
Here is a possibility using other rlang functions.
get_charts1 <- function(data, mygroup){
quo_var <- enquo(mygroup)
df_agg <- data %>%
group_by(!!quo_var) %>%
summarize(mean = mean(value, na.rm = TRUE),
count = n()) %>%
ungroup()
cc <- rlang::expr(factor(!!(rlang::get_expr(quo_var))))
# or just cc <- expr(factor(!!get_expr(quo_var))) if you include library(rlang)
ggplot(df_agg, aes_q(x = quote(count), y = quote(mean), color = cc)) +
geom_point() +
geom_line()
}
We build the expression factor(group)
using the expr()
function. We use get_expr()
to extract the symbol name "group" from the quosure quo_var
. Once we've build the expression, we can pass it on to aes_q
.
Hopefully ggplot will soon be tidy-eval-friendly and this will no longer be necessary.
这篇关于如何使用ggplot和dplyr在函数中创建因子变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!