问题描述
试图编写一个相对简单的包装器以生成一些图,但无法计算出如何指定对分组变量的整洁评估,该分组变量被定义为...
一个面向变量但不能通过分组区分的示例函数...
Trying to write a relatively simple wrapper to produce some plots, but can not work out how to specify tidy evaluation of grouping variables specified as ...
an example function that facets variables but doesn't distinguish by grouping...
my_plot <- function(df = starwars,
select = c(height, mass),
...){
results <- list()
## Tidyeval arguments
quo_select <- enquo(select)
quo_group <- quos(...)
## Filter, reshape and plot
results$df <- df %>%
dplyr::filter(!is.na(!!!quo_group)) %>%
dplyr::select(!!quo_select, !!!quo_group) %>%
gather(key = variable, value = value, !!!quo_select) %>%
## Specify what to plot
ggplot(aes(value)) +
geom_histogram(stat = 'count') +
facet_wrap(~variable, scales = 'free', strip.position = 'bottom')
return(results)
}
## Plot height and mass as facets but colour histograms by hair_color
my_plot(df = starwars, select = c(height, mass), hair_color)
它很好用,但是如何区分不同的hair_color
?通常,这是在aes()
中完成的,但是由于这是使用quos()
(即quo_group
)的结果,因此(我认为)我应该使用aes_()
代替
Great it works, but how to distinguish between different hair_color
? Normally this is done within aes()
but since this is using the results of quos()
(i.e. quo_group
) I should (I think) be using aes_()
instead
my_plot <- function(df = starwars,
select = c(height, mass),
...){
results <- list()
## Tidyeval arguments
quo_select <- enquo(select)
quo_group <- quos(...)
## Filter, reshape and plot
results$df <- df %>%
dplyr::filter(!is.na(!!!quo_group)) %>%
dplyr::select(!!quo_select, !!!quo_group) %>%
gather(key = variable, value = value, !!!quo_select) %>%
## Specify what to plot, including colouring by the supplied ... groupings
ggplot(aes_(~value, colour = !!!quo_group)) +
geom_histogram(stat = 'count') +
facet_wrap(~variable, scales = 'free', strip.position = 'bottom')
return(results)
}
## Plot height and mass as facets but colour histograms by hair_color
my_plot(df = starwars, select = c(height, mass), hair_color)
Error in !quo_group : invalid argument type
我看不到或无法阅读用dplyr编程多次.我要去哪里错了.
I can't see or work out having read Programming with dplyr several times now where I'm going wrong.
有人可以指出我的错误/告诉我的方式吗?
Can anyone point out my error/show me the way?
推荐答案
新发布的 ggplot2 v3.0.0
在aes()
中支持!!
.经过一些小的修改,您的功能现在可以正常工作
The new released ggplot2 v3.0.0
supports !!
inside aes()
. With some minor modification, your function is now working
library(tidyverse)
my_plot <- function(df = starwars,
select = c(height, mass),
...){
results <- list()
## Tidyeval arguments
quo_select <- enquo(select)
# only need quo here, if quos is used then we need to `unlist` to
# convert its output from list to vector
quo_group <- quo(...)
## Filter, reshape and plot
results$df <- df %>%
dplyr::filter(!is.na(!!!quo_group)) %>%
dplyr::select(!!quo_select, !!!quo_group) %>%
gather(key = variable, value = value, !!!quo_select) %>%
## Specify what to plot, including coloring by the supplied dots `...`
ggplot(aes(value, color = !!quo_group, fill = !!quo_group)) + # unquote inside aes
geom_histogram(stat = 'count') +
facet_wrap(vars(variable), scales = 'free', strip.position = 'bottom')
return(results)
}
## Plot height and mass as facets but color histograms by hair_color
my_plot(df = starwars, select = c(height, mass), hair_color)
由 reprex程序包(v0.2.0.9000)创建.
Created on 2018-09-12 by the reprex package (v0.2.0.9000).
这篇关于简洁的评估程序和ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!