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)我的目标是一个2×2的格子图,上面一行显示通货膨胀率和失业率,然后在下两行显示这些地块的不同版本。
问题是,当我把失业和通货膨胀相提并论时,它会改变名单的长度,然后与年数不再匹配。
因此,我得到以下错误:
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)发布于 2022-03-08 09:37:10
我认为bug在你的样本数据中。密码很好用。我添加了infl和unem变量。您可以使用例如length(diff(infl))来检查变量的长度。diff返回的长度小于组的长度。
样本数据:
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) 样本代码:
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)情节:

https://stackoverflow.com/questions/71386973
复制相似问题