将标签传递给ggplot2中的xlab和ylab [英] Passing labels to xlab and ylab in ggplot2
问题描述
我创建了一个函数,目标是以矢量化方式创建一系列绘图.这些功能部分地实现了我想要的功能,即根据所选变量更新绘图.但是,我无法传递label参数(即label_x和label_y),从而无法始终更新xlab和ylab.
I have created a function where the objective is to create a series of plots in a vectorzation way. The functions partially does what I want which is update update the plot based on the selected variables. However, I am not able to pass the label argument (i.e. label_x and label_y) so that the xlab and ylab are updated consistently.
library(tidyverse)
plot_scatter_with_label <- function(df,
var_x,
var_y,
label_x,
label_y,
geom_smooth = FALSE,
point_shape = 16,
point_color = "#EB3300",
point_size = 1,
point_alpha = 1,
smooth_method = "loess",
smooth_se = FALSE,
smooth_color = "navy") {
df <- data.frame(lapply(df, function(x) as.numeric(as.character(x))))
scatter_plot <- function(x, y) {
p <- ggplot(df, aes_string(x = x, y = y)) +
geom_point(shape = point_shape, color = point_color, size = point_size, alpha = point_alpha) +
ylab(label_y) + xlab(label_x)
p
}
map2(
var_y, label_y,
~ map(var_x, scatter_plot, y = .x)
)
}
示例
plot_scatter_with_label(
df = mtcars,
var_y = c("mpg", "hp"),
label_y = c("Miles per gallon [Mpg]", "Horse power [CV]"),
var_x = c("cyl", "gear"),
label_x = c("Cylinders [n]", "Gear [n]")
)
我原本希望获得以下情节:
I was expecting to obtain the following plots:
1)mpg vs cyl
1) mpg vs cyl
2)mpg vs装备
2) mpg vs gear
3)hp vs cyl
3) hp vs cyl
4)hp vs gear
4) hp vs gear
看来我得到了这4个图,但是标签没有按预期更新.它总是返回在label_x和label_y中定义的fist参数.
It appears that I got these 4 plots but the labels are not updated as expected. It always returns the fist argument of defined in label_x and label_y.
任何帮助将不胜感激.
最诚挚的问候
推荐答案
我们可以使用pmap
或pwalk
将数据传递给plot_scatter_with_label
函数
We can use pmap
or pwalk
to pass data to plot_scatter_with_label
function
library(tidyverse)
plot_scatter_with_label <- function(dat,
var_x,
var_y,
label_x,
label_y,
geom_smooth = FALSE,
point_shape = 16,
point_color = "#EB3300",
point_size = 1,
point_alpha = 1,
smooth_method = "loess",
smooth_se = FALSE,
smooth_color = "navy") {
if (is.character(var_x)) {
print('character column names supplied, use rlang::sym()')
var_x <- rlang::sym(var_x)
} else {
print('bare column names supplied, use dplyr::enquo()')
var_x <- enquo(var_x)
}
if (is.character(var_y)) {
var_y <- rlang::sym(var_y)
} else {
var_y <- enquo(var_y)
}
p <- ggplot(dat, aes(x = !! var_x, y = !! var_y)) +
geom_point(shape = point_shape, color = point_color,
size = point_size, alpha = point_alpha) +
ylab(label_y) +
xlab(label_x) +
ggtitle(paste0(label_x, " ~ ", label_y))
print(p)
}
创建一个数据框,以便我们可以遍历每一行和每一列
Create a data frame so that we can loop through every row and column
var_y = c("mpg", "hp")
label_y = c("Miles per gallon [Mpg]", "Horse power [CV]")
var_x = c("cyl", "gear")
label_x = c("Cylinders [n]", "Gear [n]")
var_xy <- expand.grid(var_x, var_y, stringsAsFactors = FALSE)
label_xy <- expand.grid(label_x, label_y, stringsAsFactors = FALSE)
select_dat <- data.frame(var_xy, label_xy, stringsAsFactors = FALSE)
str(select_dat)
#> 'data.frame': 4 obs. of 4 variables:
#> $ Var1 : chr "cyl" "gear" "cyl" "gear"
#> $ Var2 : chr "mpg" "mpg" "hp" "hp"
#> $ Var1.1: chr "Cylinders [n]" "Gear [n]" "Cylinders [n]" "Gear [n]"
#> $ Var2.1: chr "Miles per gallon [Mpg]" "Miles per gallon [Mpg]" "Horse power [CV]" "Horse power [CV]"
将每一行传递给plot_scatter_with_label
函数
pwalk(select_dat, ~ plot_scatter_with_label(mtcars, ..1, ..2, ..3, ..4))
#> [1] "character column names supplied, use rlang::sym()"
#> [1] "character column names supplied, use rlang::sym()"
#> [1] "character column names supplied, use rlang::sym()"
#> [1] "character column names supplied, use rlang::sym()"
由 reprex软件包(v0.2.1.9000)创建于2019-02-14
Created on 2019-02-14 by the reprex package (v0.2.1.9000)
这篇关于将标签传递给ggplot2中的xlab和ylab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!