I have read http://dplyr.tidyverse.org/articles/programming.html about non standard evaluation in dplyr but still can't get things to work.
我已经阅读了http://dplyr.tidyverse.org/articles/programming.html关于dplyr中的非标准评估,但仍然无法使用。
plot_column <- "columnA"
plot_column < - “columnA”
raw_data %>%
group_by(.dots = plot_column) %>%
summarise (percentage = mean(columnB)) %>%
filter(percentage > 0) %>%
arrange(percentage) %>%
# mutate(!!plot_column := factor(!!plot_column, !!plot_column))%>%
ggplot() + aes_string(x=plot_column, y="percentage") +
geom_bar(stat="identity", width = 0.5) +
coord_flip()
works fine when the mutate statement is disabled. However, when enabling it in order to order the bars by height only a single bar is returned.
禁用mutate语句时工作正常。但是,当启用它以按高度排序时,只返回一个条。
How can I convert the statement above into a function / to use a variable but still plot multiple bars ordered by their size.
如何将上述语句转换为函数/使用变量,但仍然按照大小排列多个条形图。
An example Dataset could be:
数据集的示例可能是:
columnA,columnB
a, 1
a, 0.4
a, 0.3
b, 0.5
edit
a sample:
一个样品:
mtcars %>%
group_by(mpg) %>%
summarise (mean_col = mean(cyl)) %>%
filter(mean_col > 0) %>%
arrange(mean_col) %>%
mutate(mpg := factor(mpg, mpg))%>%
ggplot() + aes(x=mpg, y=mean_col) +
geom_bar(stat="identity")
coord_flip()
will output an ordered bar chart. How can I wrap this into a function where the column can be replaced and I get multiple bars?
将输出有序的条形图。如何将其包装到可以替换列的函数中并且我得到多个条形图?
1 个解决方案
#1
2
This works with dplyr 0.7.0 and ggplot 2.2.1:
这适用于dplyr 0.7.0和ggplot 2.2.1:
rm(list = ls())
library(ggplot2)
library(dplyr)
raw_data <- tibble(columnA = c("a", "a", "b", "b"), columnB = c(1, 0.4, 0.3, 0.5))
plot_col <- function(df, plot_column, val_column){
pc <- enquo(plot_column)
vc <- enquo(val_column)
pc_name <- quo_name(pc) # generate a name from the enquoted statement!
df <- df %>%
group_by(!!pc) %>%
summarise (percentage = mean(!!vc)) %>%
filter(percentage > 0) %>%
arrange(percentage) %>%
mutate(!!pc_name := factor(!!pc, !!pc)) # insert pc_name here!
ggplot(df) + aes_(y = ~percentage, x = substitute(plot_column)) +
geom_bar(stat="identity", width = 0.5) +
coord_flip()
}
plot_col(raw_data, columnA, columnB)
plot_col(mtcars, mpg, cyl)
Problem I ran into was kind of that ggplot and dplyr use different kinds of non-standard evaluation. I got the answer at this question: Creating a function using ggplot2 .
我碰到的问题是那种ggplot和dplyr使用不同种类的非标准评估。我在这个问题上得到了答案:使用ggplot2创建一个函数。
EDIT: parameterized the value column (e.g. columnB/cyl) and added mtcars example.
编辑:参数化值列(例如columnB / cyl)并添加mtcars示例。
#1
2
This works with dplyr 0.7.0 and ggplot 2.2.1:
这适用于dplyr 0.7.0和ggplot 2.2.1:
rm(list = ls())
library(ggplot2)
library(dplyr)
raw_data <- tibble(columnA = c("a", "a", "b", "b"), columnB = c(1, 0.4, 0.3, 0.5))
plot_col <- function(df, plot_column, val_column){
pc <- enquo(plot_column)
vc <- enquo(val_column)
pc_name <- quo_name(pc) # generate a name from the enquoted statement!
df <- df %>%
group_by(!!pc) %>%
summarise (percentage = mean(!!vc)) %>%
filter(percentage > 0) %>%
arrange(percentage) %>%
mutate(!!pc_name := factor(!!pc, !!pc)) # insert pc_name here!
ggplot(df) + aes_(y = ~percentage, x = substitute(plot_column)) +
geom_bar(stat="identity", width = 0.5) +
coord_flip()
}
plot_col(raw_data, columnA, columnB)
plot_col(mtcars, mpg, cyl)
Problem I ran into was kind of that ggplot and dplyr use different kinds of non-standard evaluation. I got the answer at this question: Creating a function using ggplot2 .
我碰到的问题是那种ggplot和dplyr使用不同种类的非标准评估。我在这个问题上得到了答案:使用ggplot2创建一个函数。
EDIT: parameterized the value column (e.g. columnB/cyl) and added mtcars example.
编辑:参数化值列(例如columnB / cyl)并添加mtcars示例。