粘贴列 header 作为轴标签
pasting column header as axis label
我有一个大数据框。我想使用函数快速绘制每一列的图形。我无法让 Y 轴标签匹配列 header。
这是一个示例数据框。假设我想创建两个图形,名称 1 和名称 2 作为每个图形的 y 轴。
dat <- data.frame(X = c(1, 2, 3, 4),
name1 = c(50, 100, 200, 250),
name2 = c(10, 20, 30, 40))
我写了一个函数来创建我的图表
plot <- function (dat, col) {
ggplot(dat, aes(x = X, y= {{col}}, group=1))+
geom_point(size = 1)+
geom_line(size = 0.5)+
theme(axis.text.y = paste0({{col}}, "plus units and other text"))
}
然后我想为列 "name1"
创建图表
plot(dat, name1)
如果我从我的函数代码中删除主题行,这将起作用。我想Y轴表示"name1 plus units and other text."
您可以使用 aes_string
并将 name1 参数作为字符串提供给函数。您也可以使用 labs
来命名轴标题。
plot <- function(dat, col) {
ggplot(dat, aes(x = X)) +
geom_point(aes_string(y=col)) +
geom_line(aes_string(y=col)) +
labs(y = paste0(col, "plus units and other text"))
}
plot(dat, 'name2')
在@camilles 评论后编辑。
第二个选项:
plotB <- function (dat, col) {
colA <- enquo(col)
ggplot(dat, aes(x = X, y= !!colA))+
geom_point(size = 1)+
geom_line(size = 0.5) +
ylab(paste(quo_name(colA), ' Units'))
}
plotB(dat, name2)
如果你想为col
使用一个变量,那么在函数内部,你可以做substitute()来捕获col
的表达式,注意它应该是ylab而不是theme (..) 用于更改 y 标签:
plot <- function (dat, col) {
ggplot(dat, aes(x = X, y= {{col}}, group=1))+
geom_point(size = 1)+
geom_line(size = 0.5)+
ylab(substitute(col))
#ylab()
}
如果您需要将更复杂的表达式附加到您的 ylab,请执行以下操作:
plot <- function (dat, col) {
LAB=substitute(V:Ca ~ (µmol ~ mol^{-1}) , list(V = substitute(col)))
ggplot(dat, aes(x = X, y= {{col}}, group=1))+
geom_point(size = 1)+
geom_line(size = 0.5)+
ylab(LAB)
}
请查看 this chapter on quasiquotations 以更好地使用它们。
我有一个大数据框。我想使用函数快速绘制每一列的图形。我无法让 Y 轴标签匹配列 header。
这是一个示例数据框。假设我想创建两个图形,名称 1 和名称 2 作为每个图形的 y 轴。
dat <- data.frame(X = c(1, 2, 3, 4),
name1 = c(50, 100, 200, 250),
name2 = c(10, 20, 30, 40))
我写了一个函数来创建我的图表
plot <- function (dat, col) {
ggplot(dat, aes(x = X, y= {{col}}, group=1))+
geom_point(size = 1)+
geom_line(size = 0.5)+
theme(axis.text.y = paste0({{col}}, "plus units and other text"))
}
然后我想为列 "name1"
创建图表 plot(dat, name1)
如果我从我的函数代码中删除主题行,这将起作用。我想Y轴表示"name1 plus units and other text."
您可以使用 aes_string
并将 name1 参数作为字符串提供给函数。您也可以使用 labs
来命名轴标题。
plot <- function(dat, col) {
ggplot(dat, aes(x = X)) +
geom_point(aes_string(y=col)) +
geom_line(aes_string(y=col)) +
labs(y = paste0(col, "plus units and other text"))
}
plot(dat, 'name2')
在@camilles 评论后编辑。 第二个选项:
plotB <- function (dat, col) {
colA <- enquo(col)
ggplot(dat, aes(x = X, y= !!colA))+
geom_point(size = 1)+
geom_line(size = 0.5) +
ylab(paste(quo_name(colA), ' Units'))
}
plotB(dat, name2)
如果你想为col
使用一个变量,那么在函数内部,你可以做substitute()来捕获col
的表达式,注意它应该是ylab而不是theme (..) 用于更改 y 标签:
plot <- function (dat, col) {
ggplot(dat, aes(x = X, y= {{col}}, group=1))+
geom_point(size = 1)+
geom_line(size = 0.5)+
ylab(substitute(col))
#ylab()
}
如果您需要将更复杂的表达式附加到您的 ylab,请执行以下操作:
plot <- function (dat, col) {
LAB=substitute(V:Ca ~ (µmol ~ mol^{-1}) , list(V = substitute(col)))
ggplot(dat, aes(x = X, y= {{col}}, group=1))+
geom_point(size = 1)+
geom_line(size = 0.5)+
ylab(LAB)
}
请查看 this chapter on quasiquotations 以更好地使用它们。