r - 在 ggplot aes 中使用 .$ 访问器
问题描述
我希望有人可以向我解释为什么在 ggplot aes() 中使用 .$ 访问器会破坏我的情节的细节。
# setup dataframe to plot
library(tidyverse)
library(ggplot2)
library(reshape2)
df <- data.frame(
'x' = seq(0, 99, 5),
'a' = sample(seq(0, 10), size = 20, replace = TRUE),
'b' = sample(seq(5, 15), size = 20, replace = TRUE),
'c' = rep(c(1, 2, 3, 4))
)
melt_df <- df %>%
melt(df, id.vars = c('x', 'c'), measure.vars = c('a', 'b'), variable.name = 'var')
> head(melt_df)
x c var value
1 0 1 a 10
2 5 2 a 10
3 10 3 a 9
4 15 4 a 10
5 20 1 a 8
6 25 2 a 3
现在我们绘制:
# produce broken plot using .$
melt_df %>%
ggplot(aes(
x = .$x,
y = .$value,
color = .$var)) +
geom_point() +
geom_line() +
facet_grid(. ~ c)
产生:
但这有效:
# produce working plot
melt_df %>%
ggplot(aes(
x = x,
y = value,
color = var)) +
geom_point() +
geom_line() +
facet_grid(. ~ c)
生产:
为什么?
谢谢!
解决方案
推荐阅读
- c++ - GNU order-statistics-tree 的意外结果
- java - 休眠关系在注释方式上找不到外键
- python-3.x - 给出错误:TypeError:需要一个类似字节的对象,而不是“str”
- sql - 将 oracle 的 listagg 转换为 postgres
- c++ - 检测传递给函数模板函数参数的特定函数
- java - 无法访问语言环境消息
- hive - 我可以将集群 A 中的 Hive 表与集群 B 中的 Hbase 表连接起来吗
- java - 动态防止某些字段被 Jackson 的 PropertyFilter 序列化
- python - Collapsing dictionary by merging matching keys and key,value pairs
- c# - 是否应该创建一个模型来支持存储库读取用于 UI 选择列表的数据库视图?