'Here is some sample data:

year <- list(2000, 2001, 2002, 2003, 2004, 2005)
inflation <- list(7.9, 8.3, 4.2, 5.6, 1.2)
unemployment <- list(3.3, 3.7, 2.6, 4.0) 

require(gridExtra)
plot1<-qplot(x=year, y=inflation, geom ='path', xlab='Year', ylab='Inflation Rate (%)', main = 'Inflation Rate UK 1971-2019')
plot2<-qplot(x=year, y=unemployment, geom='path', xlab='Year', ylab='Unemployment Rate (%)', main= 'Unemployment Rate')
plot3<-qplot(x=year, y=diff(infl), geom='path', xlab='Year', ylab='% change Inflation', main='Year on year difference in inflation rate', col='red')
plot4<-qplot(x = year, y=diff(unem), geom='path', xlab='Year', ylab='% change in Unemployment', main= 'Year on year difference in unemployment rate', col='blue')
grid.arrange(plot1, plot2, plot3, plot4, ncol=2, nrow=2)

I am aiming for a 2 by 2 grid of plots showing the inflation and unemployment rates in the top row and then the differenced version of these plots in the bottom two rows.

The problem is when I difference the unemployment or inflation it will change the length of list and it then no longer matches with the number of years.

So I get the following error:

Error: Aesthetics must be either length 1 or the same as the data (49): y
Backtrace:
  1. gridExtra::grid.arrange(...)
  2. gridExtra::arrangeGrob(...)
  3. base::lapply(grobs[toconv], ggplot2::ggplotGrob)
  4. ggplot2:::FUN(X[[i]], ...)
  7. ggplot2:::ggplot_build.ggplot(x)
  8. ggplot2:::by_layer(function(l, d) l$compute_aesthetics(d, plot))
  9. ggplot2:::f(l = layers[[i]], d = data[[i]])
 10. l$compute_aesthetics(d, plot)
 11. ggplot2:::f(..., self = self)
 12. ggplot2:::check_aesthetics(evaled, n)


Solution 1:[1]

I think the bug is in your sample data. The code works fine. I added infl and unem variable. You can check the length of your variable by using for example length(diff(infl)). diff returns a length one less than the length of the group.

Sample data:

 year <- c(2000, 2001, 2002, 2003, 2004, 2005)
    inflation <- c(7.9, 8.3, 4.2, 5.6, 1.2, 1.4)
    unemployment <- c(3.3, 3.7, 2.6, 4.0,5.0, 6.0) 
    infl<- c(69, 73, 32, 46, 2, 4, 7)
    unem <- c(2.3, 2.7, 1.6, 3.0,4.0, 5.0, 8) 

Sample code:

require(gridExtra)

    plot1<-qplot(x=year, y=inflation, geom ='path', xlab='Year', ylab='Inflation Rate (%)', main = 'Inflation Rate UK 1971-2019')
    plot2<-qplot(x=year, y=unemployment, geom='path', xlab='Year', ylab='Unemployment Rate (%)', main= 'Unemployment Rate')
    plot3<-qplot(x=year, y=diff(infl), geom='path', xlab='Year', ylab='% change Inflation', main='Year on year difference in inflation rate', col='red')
    plot4<-qplot(x = year, y=diff(unem), geom='path', xlab='Year', ylab='% change in Unemployment', main= 'Year on year difference in unemployment rate', col='blue')
    grid.arrange(plot1, plot2, plot3, plot4, ncol=2, nrow=2)

Plot:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rfanatic