首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以下是一些示例数据:

以下是一些示例数据:
EN

Stack Overflow用户
提问于 2022-03-07 20:36:45
回答 1查看 48关注 0票数 0
代码语言:javascript
复制
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的格子图,上面一行显示通货膨胀率和失业率,然后在下两行显示这些地块的不同版本。

问题是,当我把失业和通货膨胀相提并论时,它会改变名单的长度,然后与年数不再匹配。

因此,我得到以下错误:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-03-08 09:37:10

我认为bug在你的样本数据中。密码很好用。我添加了influnem变量。您可以使用例如length(diff(infl))来检查变量的长度。diff返回的长度小于组的长度。

样本数据:

代码语言:javascript
复制
 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) 

样本代码:

代码语言:javascript
复制
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)

情节:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71386973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档