R ggplot:按索引指定 aes
ggplot() +
layer(
data = diamonds, mapping = aes(x = carat, y = price),
geom = "point", stat = "identity"
)
在上面的示例中,我想知道是否可以通过索引指定“aes”函数的参数。
我知道克拉和价格对应于钻石名称数组中的第 1 个和第 8 个元素。您能解释一下为什么以下不起作用吗?
ggplot() +
layer(
data = diamonds, mapping = aes(x = names(diamonds)[1], y = names(diamonds)[8]),
geom = "point", stat = "identity"
)
谢谢,德里克
ggplot() +
layer(
data = diamonds, mapping = aes(x = carat, y = price),
geom = "point", stat = "identity"
)
In the above example, I am wondering if I can specify the parameters for the "aes" function by indexes.
I know that carat and price correspond to the 1st and 8th elements in the names array of diamond. Can you explain why the following does not work?
ggplot() +
layer(
data = diamonds, mapping = aes(x = names(diamonds)[1], y = names(diamonds)[8]),
geom = "point", stat = "identity"
)
Thanks, Derek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个版本不起作用,因为
names(diamonds)[1]
是"carat"
而不是carat
。使用aes_string
而不是aes
来实现此目的。编辑:
要处理包含非法字符的名称,您必须将它们括在反引号中(任何时候您想使用它们时都是如此):
The second version does not work because
names(diamonds)[1]
is"carat"
and notcarat
. Useaes_string
instead ofaes
for this to work.EDIT:
To deal with names that have illegal characters, you have to do enclose them in backticks (that's the case any time you want to use them):