2017-02-09 44 views
1

我一直在嘗試一段時間來解決格子圖的問題。我的面板數量不平,所以如果我使用非交替軸標籤,其中一個面板沒有X軸標籤。如何更改格子圖中單個面板上的軸

下面的代碼是不是我的具體項目,而是使用的R數據集是提供給所有:

densityplot(~Taste|Wine,data=WineTasting, as.table=TRUE, 
     scales=list(alternating=FALSE), 
     panel=function(x,y,...){ 
      panel.densityplot(x,plot.points=FALSE) 
      } 
) 

而這將產生一個陰謀,看起來像第一個圖像: Image 1

見如何「葡萄酒B」沒有X軸。我希望所有面板的底部都有軸刻度標記和數字標籤。所以,如果我插入panel.axis(側=「底部」)我得到一個錯誤消息,並且看起來像第二圖像的情節: Image 2

我也試過下面的代碼:

densityplot(~Taste|Wine,data=WineTasting, as.table=TRUE, 
     scales=list(alternating=FALSE), 
     panel=function(x,y,...){ 
      panel.densityplot(x,plot.points=FALSE) 
      }, 
     if(isTRUE(panel.number(2))){ 
      panel.axis(side="bottom") 
     } 
) 

但是,這只是產生了一條錯誤信息:

「錯誤GET(」 lattice.status 「ENVIR = .LatticeEnv)[前綴]] [[名]: 下標越界」

任何人都可以幫助我獲得一個軸在單一面板(葡萄酒B)的底部?

謝謝, 馬特

回答

1

你必須得到與剪裁有點創意。

library(lattice) 

densityplot(~ mpg | cyl, data = mtcars, as.table = TRUE, 
    scales = list(alternating = FALSE), 
    par.settings = list(clip = list(panel = "off")), 
    panel = function(x, y, ...) { 
     panel.densityplot(x, plot.points = FALSE, ...) 
     if (panel.number() == 2) { 
     panel.axis(side = "bottom", outside = TRUE) 
     } 
    } 
) 

Imgur

+0

感謝約翰!這工作很好! –

+0

@MatthewSnyder - 我很高興它爲你解決。如果你認爲它解決了你的問題,請考慮接受它作爲答案。 –