R Corrgram显示具有零丰度的频率对'Pie Method'

时间:2016-10-08 19:40:30

标签: r graphics correlation

我试图使用Zuur et al (2010)可重现的R代码(下图)重现一个corrgram(下图;图1),显示水鸟种类对的丰度为零的频率。填充圆圈的颜色和数量对应于具有双零的观察比例。从左下角到右上角的对角线表示变量的观测值等于零的百分比。

我已为我的数据调整了此代码,但在运行两个数据集的代码后,我遇到了同样的问题。当我运行代码时,corrgram中的圆圈没有填充,并保持为空(下图;图2)。

然而,我很困惑为什么我会遇到这个问题。如果有人能解决为什么会这样,那么我将非常感谢您的帮助。

数据:Zuur等人(2010年)

数据太大,无法包含在此帖子中,但可以在名为 ElphickBirdData.txt 的<{3}}中找到

R代码:Zuur等(2010)

RiceField <- read.table(file="ElphickBirdData.txt", header = TRUE)

AllS <- c(
 "TUSW",     "GWFG",     "WHGO",     "CAGO",     "MALL",
 "GADW",     "GWTE",     "CITE",     "UNTE",     "AMWI",     "NOPI",
 "NOSH",     "RIDU",     "CANV",     "BUFF",     "WODU",     "RUDU",
 "EUWI",     "UNDU",     "PBGB",     "SORA",     "COOT",     "COMO",
 "AMBI",     "BCNH",     "GBHE",     "SNEG",     "GREG",     "WFIB",
 "SACR",     "AMAV",     "BNST",     "BBPL",     "KILL",     "LBCU",
 "GRYE",     "LEYE",     "LBDO",     "SNIP",     "DUNL",     "WESA",
 "LESA",     "PEEP",     "RUFF",     "UNSH",     "RBGU",     "HEGU",
 "CAGU",     "GUSP")

#Determine species richness
Richness <- colSums(RiceField[,AllS] > 0, na.rm = TRUE)

#Remove all covariates
Birds  <- RiceField[,AllS]

#To reduce the of variables in the figure, we only used the
#20 species that occured at more than 40 sites.
#As a result, N = 20. Else it becomes a mess.
 Birds2 <- Birds[, Richness > 40]
 N <- ncol(Birds2)

AllNames <- names(Birds2)
A <- matrix(nrow = N, ncol = N)

 for (i in 1:N){
    for (j in 1:N){
 A[i,j] <- sum(RiceField[,AllS[i]]==0  & RiceField[,AllS[j]]==0,  na.rm=TRUE)
     }}


A1 <- A/2035
print(A1, digits = 2)
rownames(A1) <- AllNames
colnames(A1) <- AllNames


 library(lattice)
 library(RColorBrewer)

panel.corrgram.2 <- function(x, y, z, subscripts, at = pretty(z), scale = 0.8, ...)
{
require("grid", quietly = TRUE)
x <- as.numeric(x)[subscripts]
y <- as.numeric(y)[subscripts]
z <- as.numeric(z)[subscripts]
zcol <- level.colors(z, at = at, ...)
for (i in seq(along = z))
{
lims <- range(0, z[i])
tval <- 2 * base::pi *
  seq(from = lims[1], to = lims[2], by = 0.01)
  grid.polygon(x = x[i] + .5 * scale * c(0, sin(tval)),
             y = y[i] + .5 * scale * c(0, cos(tval)),
             default.units = "native",
             gp = gpar(fill = zcol[i]))
 grid.circle(x = x[i], y = y[i], r = .5 * scale,
            default.units = "native")
     }
     }

  levelplot(A1,xlab=NULL,ylab=NULL,
            at=do.breaks(c(0.5,1.01),101),
            panel=panel.corrgram.2,
            scales=list(x=list(rot=90)),
            colorkey=list(space="top"),
            col.regions=colorRampPalette(c("red","white","blue")))

   #Grey colours
   levelplot(A1.bats,xlab=NULL,ylab=NULL,
    at=do.breaks(c(0.5,1.01),101),
    panel=panel.corrgram.2,
    scales=list(x=list(rot=90)),
    colorkey=list(space="top"),
    col.regions=colorRampPalette(c(grey(0.8),grey(0.5),grey(0.2))))

图1。 supporting materials section 图2 enter image description here

1 个答案:

答案 0 :(得分:1)

问题的原因是grid.circles涂抹grid.polygon白色。您可以通过更改grid.circlegrid.polygon的顺序(或将gp = gpar(fill=NA)添加到grid.circle())来解决此问题。

panel.corrgram.2.2 <- function(x, y, z, subscripts, at = pretty(z), scale = 0.8, ...)
{
    require("grid", quietly = TRUE)
    x <- as.numeric(x)[subscripts]
    y <- as.numeric(y)[subscripts]
    z <- as.numeric(z)[subscripts]
    zcol <- level.colors(z, at = at, ...)
    for (i in seq(along = z))
    {
        lims <- range(0, z[i])
        tval <- 2 * base::pi *
            seq(from = lims[1], to = lims[2], by = 0.01)
        grid.circle(x = x[i], y = y[i], r = .5 * scale,        # change the order
                    default.units = "native")
        grid.polygon(x = x[i] + .5 * scale * c(0, sin(tval)),
                     y = y[i] + .5 * scale * c(0, cos(tval)),
                     default.units = "native",
                     gp = gpar(fill = zcol[i]))
    }
}

levelplot(A1,xlab=NULL,ylab=NULL,
    at=do.breaks(c(0.5,1.01),101),
    panel=panel.corrgram.2.2,
    scales=list(x=list(rot=90)),
    colorkey=list(space="top"),
    col.regions=colorRampPalette(c("red","white","blue")))

enter image description here