-1

I have been trying to build a bar chart for GDP growth in UK and overlay it with a recession bands. I can do what is necessary with the bar plot but the moment I overlay with the recession bands, i get an error that a variable cannot be found.

uk.recessions.df <- read.table(textConnection(
"Peak, Trough
1857-06-01, 1858-12-01
1867-06-01, 1869-12-01
1873-10-01, 1879-03-01
1882-03-01, 1885-05-01
1887-03-01, 1888-04-01
1890-07-01, 1891-05-01
1893-01-01, 1894-06-01
1895-12-01, 1897-06-01
1919-03-01, 1921-07-01
1930-01-01, 1931-12-01
1956-04-01, 1956-08-01
1961-07-01, 1962-01-01
1973-09-01, 1974-04-01
1975-04-01, 1975-10-01
1980-01-01, 1981-04-01
1990-07-01, 1991-09-01
2008-04-01, 2009-07-01
2020-01-01, 2020-07-01"), sep=',',
colClasses=c('Date', 'Date'), header=TRUE)

uk.recessions.trim.df <- subset(uk.recessions.df, Peak >= min(tbl.QQGDP$Date))

tbl.data <- tbl.QQGDP %>%
  mutate(Value = GDPGrowth < 0) 

p <- ggplot(data = tbl.data, aes(x = Date, y = GDPGrowth, fill = Value)) + 
  geom_col(position = "identity", colour = "black", size = 0.25) + 
  scale_fill_manual(values = c("#85225f","#dbab01"), guide = FALSE) +
  theme_tq() 


p <- p + 
  geom_rect(data = uk.recessions.trim.df,
            aes(xmin = Peak, xmax = Trough, ymin = -Inf, ymax = Inf),
            fill = "grey", alpha = 0.5)

p

The error i get is Error in FUN(X[[i]], ...) : object 'GDPGrowth' not found

I am cannot figure out what i am doing wrong. Any help (even if to tell me off for a silly mistake!!) will be greatly appreciated.

  • 1
    Data is incomplete! – Duck Aug 19 '20 at 16:05
  • Can't see the issue without the `tbl.QQGDP` data – stlba Aug 19 '20 at 16:10
  • [link](https://drive.google.com/file/d/18ITYka5xH4Ux0QwY_fTvhw0dJJDx9tMf/view?usp=sharing) this is the file with the data. Thanks – AbhimanyuChatterjee Aug 20 '20 at 07:57
  • I can't access the data through that link, please paste the data here using dput() or similar. You can see some guidance [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – stlba Aug 20 '20 at 13:58
  • structure(list(Date = structure(c(1404086400, 1412035200, 1419984000, 1427760000, 1435622400, 1443571200, 1451520000, 1459382400, 1467244800, 1475193600, 1483142400, 1490918400, 1498780800, 1506729600, 1514678400, 1522454400, 1530316800, 1538265600, 1546214400, 1553990400, 1561852800, 1569801600, 1577750400, 1585612800, 1593475200), tzone = "UTC", class = c("POSIXct", "POSIXt")), GDPGrowth = c(0.7, 0.6, 0.6, 0.5, 0.7, 0.4, 0.7, 0.2, 0.5, 0.5, 0.6, 0.6, 0.3, 0.3, 0.4, 0.1, 0.5, 0.6, 0.2, 0.7, -0.1, 0.5, 0, -2.2, -20.4)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame")) – AbhimanyuChatterjee Aug 21 '20 at 10:08
  • basically the plot works for the geom_bar part, but does not when I add in the geom_rect bit. This is when i get the error mentioned. – AbhimanyuChatterjee Aug 21 '20 at 10:15

1 Answers1

3

By default, geom_*() elements inherit the aesthetic mappings from the top level of the plot (ggplot()). In your case, the geom_rect() call is inheriting aes(x = Date, y = GDPGrowth, fill = Value) but can't find those objects as you have a different data source (uk.recessions.trim.df instead of tbl.data).

If you add the option inherit.aes = FALSE to geom_rect() you'll get the desired plot.

p <- ggplot(data = tbl.data, aes(x = Date, y = GDPGrowth, fill = Value)) +
     geom_col(position = "identity", colour = "black", size = 0.25) +
     scale_fill_manual(values = c("#85225f","#dbab01"), guide = FALSE)


p <- p +
     geom_rect(data = uk.recessions.trim.df,
               aes(xmin = Peak, xmax = Trough, ymin = -Inf, ymax = Inf),
               fill = "grey", alpha = 0.5,
               inherit.aes = FALSE)

p

An alternative (and probably better method) is to define data and aes in each geom separately, instead of in the initial ggplot() call. Eg:

p <- ggplot() + 
     geom_col(data = tbl.data, 
              aes(x = Date, y = GDPGrowth, fill = Value),
              position = "identity", colour = "black", size = 0.25) + 
     scale_fill_manual(values = c("#85225f","#dbab01"), guide = FALSE) 


p <- p + 
     geom_rect(data = uk.recessions.trim.df,
               aes(xmin = Peak, xmax = Trough, ymin = -Inf, ymax = Inf),
               fill = "grey", alpha = 0.5)

p
stlba
  • 667
  • 3
  • 13
  • thanks a lot!! had no idea about this... we learn something everyday!! – AbhimanyuChatterjee Aug 24 '20 at 07:57
  • 1
    No problem! You can also define the `aes()` for each geom separately instead of putting it in the `ggplot()` call if they vary. If your problem is fixed now you can select this answer to mark the question solved – stlba Aug 24 '20 at 11:25