4

I'm trying to plot a geom_rect(). Why do I receive an Error in FUN(X[[i]], ...) : object 'Month' not found? If I run df$Month in my console the object is there:

df$Month
#> [1] 2019-01 2019-02 2019-03
#> Levels: 2019-01 2019-02 2019-03

Here's my code block:

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
Display name
  • 4,153
  • 5
  • 27
  • 75
  • 1
    I think you need to unmap x and y, by setting `x = NULL, y = NULL` in `geom_rect`, but re-reading the docs I feel like they could be clearer about this. They sort of imply that `geom_rect` doesn't accept x/y at all. – joran Mar 22 '19 at 14:36
  • 1
    It seems that your desired result should utilize `theme()` not `geom_rect()`. – OTStats Mar 22 '19 at 14:37

3 Answers3

2

This works:

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")

by unmapping the inherited x/y aesthetics from the top ggplot call. It's understandable that this might be confusing, though, since the description in ?geom_rect sorta kinda implies that geom_rect isn't looking for those aesthetics at all.

joran
  • 169,992
  • 32
  • 429
  • 468
1

I was able to return your desired result by calling df in geom_line() after gemo_rect(). However leaving the Month field as is returned the error: Error: Discrete value supplied to continuous scale.
I worked around this by wrapping as.integer() around Month.

ggplot() + 
  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") + 
  geom_line(data = df, aes(as.integer(Month), Value, group = 1))

enter image description here

You might have to clean up your x-axis label but it achieves desired outcome!

OTStats
  • 1,820
  • 1
  • 13
  • 22
1

You just have an extra step of setting up a dataframe in geom_rect which coincide with data in ggplot. Simply provide your max and min values to geom_rect and it works:

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")
             
M--
  • 25,431
  • 8
  • 61
  • 93