用purrr图打印ggplot [英] printing ggplot with purrr map
问题描述
我想针对我的响应变量
为数字列
创建 ggplots
.
这是可复制的代码:
test = mpg %>% select_if(is.numeric) %>%
dplyr::select(-year) %>% nest(-cyl) %>%
mutate(ggplots = map(data,~ggplot(data = .x) + geom_point(aes(x = cyl, y = .x))))
test
# A tibble: 4 x 3
cyl data ggplots
<int> <list<df[,3]>> <list>
1 4 [81 x 3] <gg>
2 6 [79 x 3] <gg>
3 8 [70 x 3] <gg>
4 5 [4 x 3] <gg>
Warning message:
All elements of `...` must be named.
Did you want `data = c(displ, cty, hwy)`?
得到错误:
test$ggplots[[1]]
Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (81): x, y
怎么了?
推荐答案
当我们要遍历一堆变量并将每个变量与另一个变量作图时,一个选项是遍历变量名.
One option when we want to loop through a bunch of variables and plot each of them against another variable is to loop through the variable names.
我首先要在 y
上提取所需的变量名.我在管道的末尾使用 set_names()
来命名向量本身,因为有时以后需要使用它进行组织.
I would first pull out the variable names I want on the y
. I use set_names()
at the end of the pipe to name the vector with itself, because sometimes I need that for organization later.
vars = mpg %>%
select_if(is.numeric) %>%
select(-cyl, - year) %>%
names() %>%
set_names()
结果是字符串的向量.
vars
# displ cty hwy
# "displ" "cty" "hwy"
现在,我可以遍历这些变量名,并针对固定的 x
变量 cyl
作图.我将为此使用 purrr :: map()
循环.由于我正在使用字符串,因此需要在 ggplot()
中使用整洁的评估,并通过 .data
代词完成(我相信这仅适用于最新的0.4.0版本) rlang 的发布).我在 labs()
中用变量标记y轴,否则在轴标签中具有 .data
代词.
Now I can loop through those variable names and make a plot against the fixed x
variable cyl
. I'll use a purrr::map()
loop for this. Since I'm working with strings I need to use tidy evaluation within ggplot()
, done with the .data
pronoun (I believe this only works since the latest 0.4.0 release of rlang). I label the y axis with the variable in labs()
, otherwise it has the .data
pronoun in the axis label.
plots = map(vars, ~ggplot(data = mpg) +
geom_point(aes(x = cyl, y = .data[[.x]]) ) +
labs(y = .x)
)
I demonstrate the approach above in a blog post I wrote last year if you're interested in more explanation.
如果您不想像这样遍历字符串,另一种选择是将数据集重整为长格式,然后使用嵌套方法.想法是制作一个长数据集,将所需的变量放在y轴上,并将它们的值放在一起放在单个列中.我使用 tidyr :: pivot_longer()
进行此操作.现在, y
变量的数字值位于一列中,名为 value
.
If you don't want to loop through strings like this, another option is to reshape the dataset into a long format and then use the nesting approach. The idea is to make a long dataset, taking the variables you want on the y axis and putting their values all together in a single column. I do this with tidyr::pivot_longer()
. The numeric values for the y
variables are now in a single column, named value
.
然后为每个变量名称嵌套 cyl
和 value
列.完成此操作后,您将拥有三行数据集,每个 y
变量一行,并且可以遍历 mutate()
中的数据集以创建图列就像您最初的尝试一样.
Then nest the cyl
and value
columns for each variable name. Once that is done you'll have a three row dataset, one row per y
variable, and you can loop through the datasets in mutate()
to create your column of plots as in your original attempt.
plots2 = mpg %>%
select_if(is.numeric) %>%
dplyr::select(-year) %>%
pivot_longer(cols = -cyl) %>%
nest(data = -name) %>%
mutate(ggplots = map(data,
~ggplot(data = .x) + geom_point(aes(x = cyl, y = value)))
这篇关于用purrr图打印ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!