0

私のデータセットは data.frame NewAuto で、名前は次のとおりです。

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

ggplot で 1 つの画像に 7 つのプロットを作成したいと考えています。それはより大きなコードの一部です。私の目的は、同様のプロット 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

どうすればこれを修正できますか?

初心者ですので、ご了承ください。

4

1 に答える 1

1

内で変数名を使用することはできませんaes。データ オブジェクトの要素のリテラル値または名前のみを使用できます。aes_string代わりに使用する必要があります:

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

あなたが失敗する理由は、「NewAuto [、column]」がNewAutoの列ではないためです。

于 2014-06-01T19:21:40.490 に答える