我想为数据集中的所有变量生成一系列直方图,但显然我没有正确准备数据以便在地图函数中使用。
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(.)
任何帮助是极大的赞赏。
像这样的东西也可以工作:
library(purrr)
library(dplyr)
mtcars %>%
select(wt, disp, hp) %>%
names() %>%
map(~ggplot(mtcars, aes_string(x = .)) + geom_histogram())
or:
mtcars %>%
select(wt, disp, hp) %>%
{map2(list(.), names(.), ~ ggplot(.x, aes_string(x = .y)) + geom_histogram())}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)