使用函数在R中创建用于可视化的箱线图
我写了一个代码来获取用于R可视化的箱线图。代码正在运行,但我没有得到任何箱线图。你能帮我找出哪里出了问题吗使用函数在R中创建用于可视化的箱线图,r,function,visualization,R,Function,Visualization,我写了一个代码来获取用于R可视化的箱线图。代码正在运行,但我没有得到任何箱线图。你能帮我找出哪里出了问题吗 create_boxplots <- function(x, y){ ggplot(data = forest_fires) + aes_string(x = x, y = y) + geom_boxplot() + theme(panel.background = element_rect(fill = "white")) } x_var_month &l
create_boxplots <- function(x, y){
ggplot(data = forest_fires) +
aes_string(x = x, y = y) +
geom_boxplot() +
theme(panel.background = element_rect(fill = "white"))
}
x_var_month <- names(forest_fires)[3]
y_var <- names(forest_fires)[5:12]
month_box <- map2(x_var_month, y_var, create_boxplots)
代码还可以,只是当您在函数内调用ggplot时,返回对象并将其存储在列表中。你需要把它打印出来。例如:
library(ggplot2)
library(purrr)
library(gridExtra)
create_boxplots <- function(x, y){
ggplot(data = forest_fires) +
aes_string(x = x, y = y) +
geom_boxplot() +
theme(panel.background = element_rect(fill = "white"))
}
forest_fires = data.frame(matrix(runif(1300),ncol=13))
forest_fires[,3] = factor(sample(1:12,nrow(forest_fires),replace=TRUE))
x_var_month <- names(forest_fires)[3]
y_var <- names(forest_fires)[5:12]
month_box <- map2(x_var_month, y_var, create_boxplots)
要在1个绘图中获得所有内容,请执行以下操作:
grid.arrange(grobs=month_box)
欢迎来到stackoverflow。尝试删除不必要的变量,用最简单的代码创建一个可重复的错误,以便其他人可以尝试复制相同的错误,并帮助您。最好的。除此之外,如果你能在上面加上一句话,那就更好了。
grid.arrange(grobs=month_box)