【发布时间】:2018-03-16 04:28:34
【问题描述】:
我想为数据集中的所有变量生成一系列直方图,但我显然没有正确准备数据以用于 map 函数。
library(tidyverse)
mtcars %>%
select(wt, disp, hp) %>%
map(., function(x)
ggplot(aes(x = x)) + geom_histogram()
)
我可以使用 for 循环完成此任务(h/t 但我试图在 tidyverse 中做同样的事情。
foo <- function(df) {
nm <- names(df)
for (i in seq_along(nm)) {
print(
ggplot(df, aes_string(x = nm[i])) +
geom_histogram())
}
}
mtcars %>%
select(wt, disp, hp) %>%
foo(.)
非常感谢任何帮助。
【问题讨论】:
-
您需要为每个图创建不同的图,还是分面解决方案可以?