问题描述
考虑这个简单的例子
library(dplyr)
library(ggplot2)
数据帧< - data_frame(id = c(1,2,3,4),
group = c('a','b','c','c'),
value = c(200,400,120,300))
#A tibble:4 x 3
id组值
< dbl> < CHR> < DBL>
1 1 a 200
2 2 b 400
3 3 c 120
4 4 c 300
这里我想写一个函数,它将数据框和分组变量作为输入。理想情况下,在分组和汇总之后,我想打印一个 ggpplot 图表。
这适用于:
get_charts2< - function(data,mygroup){
quo_var< - enquo(mygroup)
df_agg< - data%>%
group_by(!! quo_var)%>%
汇总(mean = mean(value,na.rm = TRUE),
count = n())%>%
ungroup()
df_agg
}
> get_charts2(dataframe,group)
#一个tibble:3 x 3
组平均数
< chr> < DBL> < INT>
1 a 200 1
2 b 400 1
3 c 210 2
不幸的是,在 FAILS
上添加 ggplot get_charts1< - 函数(数据,mygroup){
quo_var< - enquo(mygroup)
df_agg< - data%>%
group_by(!! quo_var)%>%
summary(mean = mean(value,na.rm = TRUE),
count = n())%>%
b ungroup()
ggplot(df_agg,aes(x = count,y = mean,color = !! quo_var,group = !! quo_var))+
geom_point()+
geom_line()
}
> get_charts1(dataframe,group)
错误!quo_var:无效参数类型
我不理解这里有什么问题。有任何想法吗?
Thanks!
ggplot 还不支持tidy eval语法(您不能使用! !)。您需要使用更传统的标准评估电话。您可以在ggplot中使用 aes_q 来帮助解决这个问题。
get_charts1< ; - 函数(data,mygroup){
quo_var< - enquo(mygroup)
df_agg< - data%>%
group_by(! quo_var)%>%
汇总(均值=均值(值,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()
$ b get_charts1(dataframe,group)
Consider this simple example
library(dplyr) library(ggplot2) dataframe <- data_frame(id = c(1,2,3,4), group = c('a','b','c','c'), value = c(200,400,120,300)) # A tibble: 4 x 3 id group value <dbl> <chr> <dbl> 1 1 a 200 2 2 b 400 3 3 c 120 4 4 c 300
Here I want to write a function that takes the dataframe and the grouping variable as input. Ideally, after grouping and aggregating I would like to print a ggpplot chart.
This works:
get_charts2 <- function(data, mygroup){ quo_var <- enquo(mygroup) df_agg <- data %>% group_by(!!quo_var) %>% summarize(mean = mean(value, na.rm = TRUE), count = n()) %>% ungroup() df_agg } > get_charts2(dataframe, group) # A tibble: 3 x 3 group mean count <chr> <dbl> <int> 1 a 200 1 2 b 400 1 3 c 210 2
Unfortunately, adding ggplot into the function above FAILS
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() ggplot(df_agg, aes(x = count, y = mean, color = !!quo_var, group = !!quo_var)) + geom_point() + geom_line() } > get_charts1(dataframe, group) Error in !quo_var : invalid argument type
I dont understand what is wrong here. Any ideas?Thanks!
EDIT: interesting follow-up here how to create factor variables from quosures in functions using ggplot and dplyr?
ggplot does not yet support tidy eval syntax (you can't use the !!). You need to use more traditional standard evaluation calls. You can use aes_q in ggplot to help with this.
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() ggplot(df_agg, aes_q(x = quote(count), y = quote(mean), color = quo_var, group = quo_var)) + geom_point() + geom_line() } get_charts1(dataframe, group)
这篇关于如何将ggplot和dplyr组合到一个函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!