【问题标题】:Multiple plots with grid.arrange带有 grid.arrange 的多个绘图
【发布时间】:2014-06-01 19:19:35
【问题描述】:

我的数据集是一个data.frame NewAuto,它有名字:

[1] "mpg"          "cylinders"    "displacement" "horsepower"   "weight"      
[6] "acceleration" "year"         "origin"       "name"         "MPG01"

我想用 ggplot 在一张图片上绘制七个图。它是更大代码的一部分。我的目标是制作与其他列相似的 mpg 图。每个图都应标有 y 轴。

SCATTERplots <- lapply(
2:8, 
function( column ){
  DataPlot <- ggplot(
    data = NewAuto,
    aes(
      x = mpg,
      y=NewAuto[,column]
    )
  )+geom_point()+facet_grid(.~MPG01)+ylab(names(NewAuto)[column])
  return( DataPlot)  
}
)
do.call( grid.arrange, SCATTERplots) 

不幸的是,我得到了:

Error in `[.data.frame`(NewAuto, , column) : object 'column' not found

我该如何解决这个问题?

我是初学者,所以请考虑到这一点。

【问题讨论】:

    标签: r ggplot2 gridextra


    【解决方案1】:

    您不能在aes 中使用变量名。您只能使用数据对象中的文字值或元素名称。您应该改用aes_string

    aes_string(
      x = "mpg",
      y="column"
    )
    

    您失败的原因是“NewAuto[,column]”它不是 NewAuto 中的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多