Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ggplot geom_rect()错误;“未找到对象”;_R_Ggplot2 - Fatal编程技术网

ggplot geom_rect()错误;“未找到对象”;

ggplot geom_rect()错误;“未找到对象”;,r,ggplot2,R,Ggplot2,我正在尝试绘制一个geom\u rect()。为什么我在FUN(X[[I]],…)中收到一个错误:找不到对象“Month”?如果我在控制台中运行df$Month,对象就在那里: df$Month #> [1] 2019-01 2019-02 2019-03 #> Levels: 2019-01 2019-02 2019-03 这是我的代码块: library(tidyverse) df <- tibble(Month = factor(c("2019-01", "2019-0

我正在尝试绘制一个
geom\u rect()
。为什么我在FUN(X[[I]],…)中收到一个
错误:找不到对象“Month”
?如果我在控制台中运行
df$Month
,对象就在那里:

df$Month
#> [1] 2019-01 2019-02 2019-03
#> Levels: 2019-01 2019-02 2019-03
这是我的代码块:

library(tidyverse)
df <- tibble(Month = factor(c("2019-01", "2019-02", "2019-03")), 
             Value = c(4, 9, 7))

ggplot(df, aes(Month, Value, group = 1)) + 
  geom_line() + 
  theme_minimal() + 
  geom_rect(data = 
              data.frame(xmin = min(as.integer(df$Month)) - 0.5,
                         xmax = max(as.integer(df$Month)) + 0.5,
                         ymin = min(df$Value),
                         ymax = max(df$Value)),
            aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            alpha = 0.2, fill = "green")

#> Error in FUN(X[[i]], ...) : object 'Month' not found
库(tidyverse)
FUN中的df错误(X[[i]],…):未找到对象“Month”

gemo_rect()
之后调用
geom_line()
中的
df
可以返回您想要的结果。但是,将月份字段保留为返回的错误:错误:提供给连续刻度的离散值
我通过将
包装为.integer()
大约一个月来解决这个问题

您可能需要清理您的x轴标签,但它达到了预期的结果

这是有效的:

ggplot(df, aes(Month, Value, group = 1)) + 
  geom_line() + 
  theme_minimal() + 
  geom_rect(data = 
              data.frame(xmin = min(as.integer(df$Month)) - 0.5,
                         xmax = max(as.integer(df$Month)) + 0.5,
                         ymin = min(df$Value),
                         ymax = max(df$Value)),
            aes(x = NULL,y = NULL,xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            alpha = 0.2, fill = "green")

通过从顶部ggplot调用取消映射继承的x/y美学。不过,这可能会令人困惑,这是可以理解的,因为
?geom_\u rect
排序中的描述有点暗示
geom_\u rect
根本不寻求这些美学。

您只需在
geom_\u rect
中设置一个与
ggplot
中的数据一致的数据框。只需将您的最大值和最小值提供给
geom_rect
即可:

ggplot(df, aes(Month, Value, group = 1)) + 
  geom_line() + 
  theme_minimal() + 
  geom_rect(aes(xmin = min(as.integer(Month)) - 0.5, 
                xmax = max(as.integer(Month)) + 0.5, 
                ymin = min(Value), 
                ymax = max(Value)),
            alpha = 0.2/nrow(df), fill = "green")

我认为您需要通过在
geom\u rect
中设置
x=NULL,y=NULL
来取消x和y的映射,但是重新阅读文档时,我觉得他们可以更清楚地了解这一点。它们有点暗示
geom\u rect
根本不接受x/y。似乎您想要的结果应该使用
theme()
而不是
geom\u rect()
继承。aes=F也可以工作,而不是将
x
y
设置为
NULL
ggplot(df, aes(Month, Value, group = 1)) + 
  geom_line() + 
  theme_minimal() + 
  geom_rect(aes(xmin = min(as.integer(Month)) - 0.5, 
                xmax = max(as.integer(Month)) + 0.5, 
                ymin = min(Value), 
                ymax = max(Value)),
            alpha = 0.2/nrow(df), fill = "green")