如何在ggplot中使用变量指定列名
我
ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) )
在函数中有一个 ggplot 命令。但我希望能够使用函数的参数来挑选要用作颜色和组的列。即我想要这样的东西
f <- function( column ) {
...
ggplot( rates.by.groups, aes(x=name, y=rate, colour= ??? , group=??? ) )
}
,以便 ggplot 中使用的列由参数确定。例如,对于 f("majr") 我们得到的效果是
ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) )
,但对于 f("gender") 我们得到的效果是
ggplot( rates.by.groups, aes(x=name, y=rate, colour=gender, group=gender) )
一些我尝试过的东西:
ggplot( rates.by.groups, aes(x=name, y=rate, colour= columnName , group=columnName ) )
不起作用。也没有
e <- environment()
ggplot( rates.by.groups, aes(x=name, y=rate, colour= columnName , group=columnName ), environment=e )
I have a ggplot command
ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) )
inside a function. But I would like to be able to use a parameter of the function to pick out the column to use as colour and group. I.e. I would like something like this
f <- function( column ) {
...
ggplot( rates.by.groups, aes(x=name, y=rate, colour= ??? , group=??? ) )
}
So that the column used in the ggplot is determined by the parameter. E.g. for f("majr") we get the effect of
ggplot( rates.by.groups, aes(x=name, y=rate, colour=majr, group=majr) )
but for f("gender") we get the effect of
ggplot( rates.by.groups, aes(x=name, y=rate, colour=gender, group=gender) )
Some things I tried:
ggplot( rates.by.groups, aes(x=name, y=rate, colour= columnName , group=columnName ) )
did not work. Nor did
e <- environment()
ggplot( rates.by.groups, aes(x=name, y=rate, colour= columnName , group=columnName ), environment=e )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
注意:此答案中的解决方案是“soft-deprecated”。请使用
.data[[
查看当前首选方法的答案。您可以使用
aes_string
:只要将该列作为字符串(
f("majr")
而不是f(majr)
)。另请注意,我们将其他列"name"
和"rate"
更改为字符串。如果出于某种原因您不想使用
aes_string
,您可以将其更改为(稍微麻烦一些):Note: the solution in this answer is "soft-deprecated". See the answer below using
.data[[
for the currently preferred method.You can use
aes_string
:as long as you pass the column to the function as a string (
f("majr")
rather thanf(majr)
). Also note that we changed the other columns,"name"
and"rate"
, to be strings.If for whatever reason you'd rather not use
aes_string
, you could change it to (the somewhat more cumbersome):来自
ggplot2 V3.0.0发行说明 >:
现在惯用的方法是使用
sym()
将变量包含的字符串转换为符号(这与基本别名as.name()
几乎相同) /as.symbol()
),并使用!!
取消引用它模拟 OP 的数据,我们可以这样做:
如果我们宁愿将原始名称提供给函数,我们可以这样做:
它将与名称(又名符号)和字符串一起使用正如
Lionel 关于
ensym()
所说:关于
enquo()
的注释enquo()
引用表达式(不一定是符号)对于参数,它不会像 ensym() 那样将字符串文字转换为符号,因此这里可能不太适合,但我们可以这样做:From the release notes of
ggplot2 V3.0.0
:The idiomatic way now would be to convert to a symbol the string that the variable contains, using
sym()
(which is almost the same as base aliasesas.name()
/as.symbol()
), and unquote it using!!
Simulating OP's data we can do :
If we'd rather feed raw names to the function we can do:
It will work with names a.k.a. symbols AND with string literals
As Lionel says about
ensym()
:A note on
enquo()
enquo()
quotes the expression (not necessarily a symbol) fed to the argument, it doesn't convert a string literal to a symbol asensym()
does so it might be less adapted here, but we can do :另一种选择(
ggplot2 > 3.0.0
)是使用整洁的评估代词.data
从rates.by.groups
数据框中对所选变量/列进行切片。另请参阅此答案
由 reprex 包于 2019 年 4 月 4 日创建(v0.2.1.9000 )
Another option (
ggplot2 > 3.0.0
) is to use the tidy evaluation pronoun.data
to slice the chosen variable/column from therates.by.groups
data frame.See also this answer
Created on 2019-04-04 by the reprex package (v0.2.1.9000)
尝试使用
aes_string
而不是aes
。Try using
aes_string
instead ofaes
.做两件事
sym()
将列名转换为符号!!
示例
Do two things
sym()
!!
to the symbol when you want to use itExample
使用 aes_string 确实可以解决此问题,但在添加误差线 geom_errorbar 时确实会遇到问题。下面是一个简单的解决方案。
另外,您还可以使用 ggplot 中的这些行向绘图添加构面:
此脚本是根据原始帖子修改的: ggplot2 - 使用自定义函数的误差线
Using
aes_string
does fix this problem, but does face an issue when adding error barsgeom_errorbar
. Below is a simple solution.Bonus, you can also add facets to your plot using these lines inside the ggplot:
This script was modified from this original post: ggplot2 - Error bars using a custom function