2017-06-14 25 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可以使情节看起来好很多。

相关问题