在使用geom_rect创建gggraph图表时,我遇到以下错误:

Error in `geom_rect()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `FUN()`:
! object 'Weekly_Sales' not found

我的代码是:

ggplot(data = df1, aes(x = Date, y = Weekly_Sales)) +
  geom_rect(data = df2, color = 'white',aes(xmin = as.Date(xmin), 
  ymin = ymin, xmax = as.Date(xmax), ymax = ymax, fill = changes),alpha = .4) +
  geom_line(color = 'orange', size = 1.25) + 
  scale_fill_manual(values = c('blue', 'green'))

Weekly_Sales列位于df1数据集中.

推荐答案

你需要在geom_rect里面加inherit.aes = F.

inherit.aes
If FALSE, overrides the default aesthetics, rather than combining with them.

ggplot(data = df1, aes(x = Date, y = Weekly_Sales)) +
  geom_rect(data = df2, inherit.aes = F, color = 'white',aes(xmin = as.Date(xmin), 
  ymin = ymin, xmax = as.Date(xmax), ymax = ymax, fill = changes),alpha = .4) +
  geom_line(color = 'orange', size = 1.25) + 
  scale_fill_manual(values = c('blue', 'green'))

R相关问答推荐

如何在R中正确对齐放射状图中的文本

在值和NA的行顺序中寻找中断模式

R创建一个数据透视表,计算多个组的百分比

R箱形图gplot 2 4组但6个参数

找出疾病消失的受试者

名称输出pmap on tible

从所有项的 struct 相同的两级列表中,将该第二级中的所有同名项绑定在一起

展开对数比例绘图的轴(添加填充)

如何在R中平滑地绘制线图(不拟合)?

在保留列表元素属性的同时替换列表元素

汇总数据的Sheffe检验的P值(平均值和标准差)

从多个可选列中选取一个值到一个新列中

WRS2包中带有bwtrim的简单ANOVA抛出错误

如何将一些单元格的内容随机 Select 到一个数据框中?

观察器中的inaliateLater的位置

在点图上绘制置信度或预测区间ggplot2

如何删除R中除数字元素以外的所有元素

替换在以前工作的代码中有x行&q;错误(geom_sf/gganimate/dow_mark)

把代码写成dplyr中的group_by/摘要更简洁吗?

为什么在POSIXct-times的向量上循环会改变R中的类型?