2014-06-25 97 views
2

下面的代碼是從書「R在行動」,上市16.1:在R中,錯誤「需要至少一個面板」是什麼意思,以及如何解決它?

library(lattice) 
attach(mtcars) 

gear <- factor(gear, levels=c(3, 4, 5), 
       labels=c("3 gears", "4 gears", "5 gears")) 
cyl <- factor(cyl, levels=c(4, 6, 8), 
       labels=c("4 cylinders", "6 cylinders", "8 cylinders")) 

densityplot(~mpg, 
      main="Density Plot", 
      xlab="Miles per Gallon") 

densityplot(~mpg | cyl, 
      main="Density Plot by Number of Cylinders", 
      xlab="Miles per Gallon") 

bwplot(cyl ~ mpg | gear,main="Box Plots by Cylinders and Gears", 
     xlab="Miles per Gallon", ylab="Cylinders") 


xyplot(mpg ~ wt | cyl * gear, 
     main="Scatter Plots by Cylinders and Gears", 
     xlab="Car Weight", ylab="Miles per Gallon") 

cloud(mpg ~ wt * qsec | cyl, 
     main="3D Scatter Plots by Cylinders") 

dotplot(cyl ~ mpg | gear, 
     main="Dot Plots by Number of Gears and Cylinders", 
     xlab="Miles Per Gallon") 

splom(mtcars[c(1, 3, 4, 5, 6)], 
     main="Scatter Plot Matrix for mtcars Data") 

detach(mtcars) 

我只能夠繪製第一密度圖和最後的情節,splom(...)。其餘的陰謀給我以下錯誤:

Error in limits.and.aspect(default.prepanel, prepanel = prepanel, have.xlim = have.xlim, : need at least one panel

這個錯誤是什麼意思,我該如何解決它?

我剛開始學習R和我使用的版本是:

R version 3.1.0 (2014-04-10) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=English_Malaysia.1252 LC_CTYPE=English_Malaysia.1252 
[3] LC_MONETARY=English_Malaysia.1252 LC_NUMERIC=C      
[5] LC_TIME=English_Malaysia.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] lattice_0.20-29 

loaded via a namespace (and not attached): 
[1] grid_3.1.0 

編輯:

輸出搜索()是:

[1] ".GlobalEnv"  "mtcars"   "package:lattice" 
[4] "package:stats"  "package:graphics" "package:grDevices" 
[7] "package:utils"  "package:datasets" "package:methods" 
[10] "Autoloads"   "package:base" 
+2

你確定你跑了那些嗎?他們爲我工作。也許重新啓動你的R會話,然後重試。這些示例顯示了將data.frame附加到搜索路徑的錯誤做法,而不是將它作爲繪圖函數的參數,因此我不確定這是一個很好的學習方法。此外,'sessionInfo()'的輸出將比版本信息更有用。 – MrFlick

+1

如果您嘗試使用'densityplot(〜mpg | cyl,mtcars,main =「密度圖的圓柱體數量」,xlab =「每加侖數英里數」),則會發生什麼情況。那樣有用嗎?忘記附加data.frame。 – MrFlick

+0

@MFFlick是的,我跑了那些。可以肯定的是,我關閉了R,打開了它,將上面的代碼複製到一個新的腳本中,選擇了所有代碼並使用Ctrl + R運行它。錯誤仍然出現 – mauna

回答

0

我有同樣的問題當我從QuickR複製代碼。 我通過將齒輪和cyl的等級從int更改爲string來解決此問題,因爲數據集中的值都是字符串。

gear.f<-factor(gear,levels=c("3gears","4gears","5gears"), 
    labels=c("3gears","4gears","5gears")) 
cyl.f <-factor(cyl,levels=c("4cyl","6cyl","8cyl"), 
    labels=c("4cyl","6cyl","8cyl")) 
0

只需添加數據"mtcars"如下,

bwplot(cyl ~ mpg | mtcars$gear, main = "Box Plots by Cylinders and Gears", xlab = "Miles per Gallon", ylab = "Cylinders") 

它爲我工作。

相關問題