[英]Making Multiple Plots from a Function call in R studio
I am relatively new to R.我对 R 比较陌生。 My question results from a project in an online learning course.我的问题来自在线学习课程中的一个项目。 I am using R studio to make multiple plots from a function call.我正在使用 R 工作室从 function 调用中制作多个绘图。 I want a new plot for each column which represents the y-axis while the x-axis remains equal to the month.我想要一个新的 plot 代表 y 轴而 x 轴保持等于月份的每一列。 The function works when displaying a single variable. function 在显示单个变量时工作。 However, when I try the function call using multiple columns I receive:但是,当我尝试使用多列调用 function 时,我收到:
"Error: More than one expression parsed" “错误:解析了多个表达式”
Similar code worked in the online program's simulated platform.类似的代码在在线程序的模拟平台上工作。
I have provided my code below with a small sample from the data frame.我在下面提供了我的代码以及数据框中的一个小样本。 Is it possible to derive multiple plots in this way?是否有可能以这种方式导出多个图? If so, how can I update or correct my code to make the plot for each column.如果是这样,我如何更新或更正我的代码以使每列的 plot 。
month <- c('mar', 'oct', 'oct')
day <- c('fri', 'tue', 'sat')
FFMC <- c(86.2, 90.6, 90.6)
DMC <- c(26.2, 35.4, 43.7)
DC <- c(94.3, 669.1, 686.9)
ISI <- c(5.1, 6.7, 6.7)
temp <- c(8.2, 18.0, 14.6)
RH <- c(51, 33, 33)
wind <- c(6.7, 0.9, 1.3)
rain <- c(0.0, 0.0, 0.0)
forestfires_df <- data.frame(month, day, FFMC, DMC, DC, ISI, temp, RH, wind, rain)
library(ggplot2)
library(purrr)
month_box <- function(x , y) {
ggplot(data = forestfires_df, aes_string(x = month, y = y_var)) +
geom_boxplot() +
theme_bw()
}
month <- names(forestfires_df)[1]
y_var <- names(forestfires_df)[3:10]
month_plots <- map2(month, y_var, month_box)
#After running month_plots I receive "Error: More than one expression parsed"
The issue is that the function arguments should match the ones inside问题是 function arguments 应该与里面的匹配
month_box <- function(x , y) {
ggplot(data = forestfires_df, aes_string(x = x, y = y)) +
geom_boxplot() +
theme_bw()
}
If we use 'month' and 'y_var', 'y_var' is of length 8 and that is the reason we do the looping in map
.如果我们使用“月”和“y_var”,“y_var”的长度为 8,这就是我们在map
中进行循环的原因。 With the change, the map2
should work as expected随着更改, map2
应该按预期工作
map2(month, y_var, month_box)
Or using anonymous function或使用匿名 function
map2(month, y_var, ~ month_box(.x, .y))
Like I mentioned in a comment, aes_string
has been soft-deprecated in favor ofusing tidyeval to write ggplot2
functions .就像我在评论中提到的那样, aes_string
已被软性弃用,有利于使用 tidyeval 编写ggplot2
函数。 You can rewrite your function as a simple tidyeval-based one, then map over the columns of interest passing bare column names or their positions the way you would with most other tidyverse functions.您可以将 function 重写为简单的基于 tidyeval 的,然后 map 在感兴趣的列上传递裸列名称或其位置,就像使用大多数其他 tidyverse 函数一样。
There are a couple ways to write a function like this.有几种方法可以像这样编写 function。 The older way is with quosures and unquoting columns, but its syntax can be confusing.较旧的方法是使用 quosures 和 unquoting 列,但它的语法可能会令人困惑。 dplyr
comes with a very in-depth vignette, but I like this blog post as a quick guide. dplyr
带有一个非常深入的小插图,但我喜欢这篇博文作为快速指南。
month_box_quo <- function(x, y) {
x_var <- enquo(x)
y_var <- enquo(y)
ggplot(forestfires_df, aes(x = !!x_var, y = !!y_var)) +
geom_boxplot()
}
A single call looks like this, with bare column names:单个调用看起来像这样,带有裸列名称:
month_box_quo(x = month, y = DMC)
Or with map_at
and column positions (or with vars()
):或使用map_at
和列位置(或使用vars()
):
# mapped over variables of interest; assumes y gets the mapped-over column
map_at(forestfires_df, 3:10, month_box_quo, x = month)
# or with formula shorthand
map_at(forestfires_df, 3:10, ~month_box_quo(x = month, y = .))
The newer tidyeval syntax ( {{}}
, or curly-curly) is easier to follow, and returns the same list of plots as above.较新的 tidyeval 语法( {{}}
或 curly-curly)更易于遵循,并返回与上面相同的绘图列表。
month_box_curly <- function(x, y) {
ggplot(forestfires_df, aes(x = {{ x }}, y = {{ y }})) +
geom_boxplot()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.