r图形与可视化
R 报错:object 'x' not found(ggplot 图层找不到变量)
图层里的 aes() 引用了一个在 data 里不存在的列名。高频原因:数据框传了但列名拼错;或者在 geom_() 里给了一个新数据框,却没重新指定 aes()。
报错原文
Error in FUN(X[[i]], ...) : object 'x' not found为什么会报这个错
图层里的 aes() 引用了一个在 data 里不存在的列名。高频原因:数据框传了但列名拼错;或者在 geom_*() 里给了一个新数据框,却没重新指定 aes()。
怎么解决
names(df) 核对列名;每个 geom_*() 如果换了 data,要么把 aes() 写全,要么用 inherit.aes = FALSE 并重新映射。
代码示例
names(df)
# 换数据时重新映射
ggplot(df, aes(x, y)) +
geom_point() +
geom_smooth(data = df2, aes(x, y), inherit.aes = FALSE)同工具的其他报错
- R 报错:Discrete value supplied to continuous scale(离散值喂给连续标度)
- R 报错:Aesthetics must be either length 1 or the same as the data
- R 报错:stat_count() can only have an x or y aesthetic(柱状图映射错误)
- R 报错:Continuous value supplied to discrete scale(连续值喂给离散标度)
- R 报错:geom_point requires the following missing aesthetics: x and y
- R 报错:Each group consists of only one observation(拟合线画不出来)
报错解决了,但方法还没搞懂?
报错只是表象。因果推断、面板数据、稳健性检验这些方法背后的逻辑,知识库里按「原理 × 直觉 × 代码」讲清楚了。
进入知识库 →