我已经编写了一个函数,它使用ggplot2在将传递给它的数据帧的数字列上开发直方图。该函数将这些图存储到列表中,然后返回列表。
然而,当我运行该功能时,我会一次又一次得到相同的情节。
我的代码如下,我也提供了一个可重复的示例。
hist_of_columns = function(data, class, variables_to_exclude = c()){
library(ggplot2)
library(ggthemes)
data = as.data.frame(data)
variables_numeric = names(data)[unlist(lapply(data, function(x){is.numeric(x) | is.integer(x)}))]
variables_not_to_plot = c(class, variables_to_exclude)
variables_to_plot = setdiff(variables_numeric, variables_not_to_plot)
indices = match(variables_to_plot, names(data))
index_of_class = match(class, names(data))
plots = list()
for (i in (1 : length(variables_to_plot))){
p = ggplot(data, aes(x= data[, indices[i]], color= data[, index_of_class], fill=data[, index_of_class])) +
geom_histogram(aes(y=..density..), alpha=0.3,
position="identity", bins = 100)+ theme_economist() +
geom_density(alpha=.2) + xlab(names(data)[indices[i]]) + labs(fill = class) + guides(color = FALSE)
name = names(data)[indices[i]]
plots[[name]] = p
}
plots
}
data(mtcars)
mtcars$am = factor(mtcars$am)
data = mtcars
variables_to_exclude = 'mpg'
class = 'am'
plots = hist_of_columns(data, class, variables_to_exclude)
如果查看列表图,您会发现它包含重复的相同图。
答案 0 :(得分:2)
只需使用aes_string
将字符串变量传递到ggplot()
调用中。现在,您的绘图使用不同的数据源,而不是与ggplot的数据参数对齐。 x 下面,颜色和 fill 是分开的,不相关的向量,虽然它们来自相同的来源,但ggplot
不知道:
ggplot(data, aes(x= data[, indices[i]], color= data[, index_of_class], fill=data[, index_of_class]))
但是,使用 aes_string ,将字符串名称传递给 x ,颜色和 fill 将指向< EM>数据:
ggplot(data, aes_string(x= names(data)[indices[i]], color= class, fill= class))
答案 1 :(得分:1)
以下是使用tidyeval
执行您所追求的目标的策略:
library(rlang)
library(tidyverse)
hist_of_cols <- function(data, class, drop_vars) {
# tidyeval overhead
class_enq <- enquo(class)
drop_enqs <- enquo(drop_vars)
data %>%
group_by(!!class_enq) %>% # keep the 'class' column always
select(-!!drop_enqs) %>% # drop any 'drop_vars'
select_if(is.numeric) %>% # keep only numeric columns
gather("key", "value", -!!class_enq) %>% # go to long form
split(.$key) %>% # make a list of data frames
map(~ ggplot(., aes(value, fill = !!class_enq)) + # plot as usual
geom_histogram() +
geom_density(alpha = .5) +
labs(x = unique(.$key)))
}
hist_of_cols(mtcars, am, mpg)
hist_of_cols(mtcars, am, c(mpg, wt))