2017-06-14 28 views
1

我正嘗試使用R中的格子包創建圖形。我知道其他現有的包,但如果可能的話想使用格子。爲什麼格子圖中的網格線覆蓋*一些*但不是所有的數據點?

爲了給分組的xyplot添加錯誤條,我採用了Deepayan Sarkars解決方案,我找到了here(代碼如下)。

它工作正常,除非我嘗試添加網格線的情節。網格線覆蓋了一些數據點,但不是全部。有人明白這是爲什麼,以及如何避免它?我希望網格在後臺繪製。

library(lattice) 

# prepare sample data ----------------------------------------------- 
singer.split <- with(singer, 
     split(height, voice.part)) 
singer.ucl <- sapply(singer.split, 
     function(x) { 
      st <- boxplot.stats(x) 
      c(st$stats[3], st$conf)}) 

singer.ucl <- as.data.frame(t(singer.ucl)) 
names(singer.ucl) <- c("median", "lower", "upper") 
singer.ucl$voice.part <- factor(rownames(singer.ucl), 
     levels = rownames(singer.ucl)) 

singer.ucl$voice=factor(rep(c(1,2),4)) 
singer.ucl$range=factor(rep(c("Bass","Tenor","Alto","Soprano"),each=2)) 

# custom panel functions ---------------------------------------------- 
prepanel.ci <- function(x, y, ly, uy, subscripts, ...) { 
    x <- as.numeric(x) 
    ly <- as.numeric(ly[subscripts]) 
    uy <- as.numeric(uy[subscripts]) 
    list(ylim = range(y, uy, ly, finite = TRUE))} 

panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, col.line = 
         'black', ...) { 
    x <- as.numeric(x) 
    y <- as.numeric(y) 
    ly <- as.numeric(ly[subscripts]) 
    uy <- as.numeric(uy[subscripts]) 
    panel.abline(v=1:2, col = "black", lwd = 2) 
    panel.arrows(x, ly, x, uy, col = col.line, 
       length = 0.25, unit = "native", 
       angle = 90, code = 3) 
    panel.xyplot(x, y, pch = pch, col.line = col.line, ...)} 

# plot--------------------------------------------------------------- 
xyplot(median ~ voice, 
     groups=range, 
     data=singer.ucl, 
     ly = singer.ucl$lower, 
     uy = singer.ucl$upper, 
     prepanel = prepanel.ci, 
     panel = panel.superpose, 
     panel.groups = panel.ci, 
     type="p", pch = 19) 

在我的機器(MACOS,R.3.4.0,lattice_0.20-35),紅點處於黑色網格線的前面,並且被覆蓋的所有其他點:

Red point in front of grid but other points in background

幫助將不勝感激。

謝謝

康拉德

+1

當您使用「組」進行繪圖時,您基本上是在同一個面板中重新繪製,因此只有最後一個繪製點在「前面」繪製。有一些方法可以將細節添加到現有的格子圖中:'?llines','?update.trellis' –

+0

我嘗試在繪製網格線後使用latticeextra的圖層函數或通過添加其他panel.points()調用來添加箭頭或點到主面板功能。他們似乎都沒有解決這個問題。如果我在panel.arrows()和panel.xyplot()之後調用panel.abline(),則網格線將覆蓋所有內容,如預期的那樣。我無法使update()函數與繪圖一起工作來覆蓋網格線。如果解決了問題,我會高興地接受使用update()或lpoints或類似的解決方案。 – Konn

回答

2

有一個+.trellis功能包:latticeExtra通過薩卡和安德魯斯撰寫。它處理幕後所有複雜的網格調用,就像它一樣。如果將panel.ci函數的一個輕微變體的abline調用註釋掉,並將其命名爲panel.ci2,則可以覆蓋現有的圖形對象。

library(latticeExtra) # perhaps need to install first 
my.plot <- xyplot(median ~ voice, 
     groups=range, 
     data=singer.ucl, 
     ly = singer.ucl$lower, 
     uy = singer.ucl$upper, 
     prepanel = prepanel.ci, 
     panel = panel.superpose, 
     panel.groups = panel.ci, 
     type="p", pch = 19) 
myplot2 <- my.plot + xyplot(median ~ voice, 
            groups=range, 
            data=singer.ucl, 
            ly = singer.ucl$lower, 
            uy = singer.ucl$upper, 

            panel = panel.superpose, 
            panel.groups = panel.ci2, 
            type="p", pch = 19) 
png(); print(myplot2) ; dev.off() 

enter image description here

調試的注意事項。我第一次嘗試在現在有panel.ci2的地方使用panel.xyplot,只有實心點出現在前面,這讓我意識到我也需要面板函數中的箭頭。我認爲使用lty = 3lwd=0.5abline-call可以使情節看起來好很多。

相關問題