Alice Hobbs - 6 months ago 31

R Question

I am attempting to reproduce a corrgram (below; Fig 1) using Zuur et al (2010) reproducible R code (below) showing the frequency with which pairs of water- bird species both have zero abundance. The colour and the amount that a circle has been filled correspond to the proportion of observa- tions with double zeros. The diagonal running from bottom left to top right represents the percentage of observations of a variable equal to zero..

I have adapted this code for my data but I am experiencing the same problem after running the code for both datasets. When I run the code, the circles inside the corrgram are not filling in, and remain empty (below; Figure 2).

I am however confused as to why I am hitting this problem. If anyone has a solution as to why then I would be deeply appreciative for your help.

**Data: By Zuur et al (2010)**

The data is too large to include with this post but it can be found in the supporting materials section called **ElphickBirdData.txt**

**R Code: Zuur et al (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))))

Answer

The cause of your problem is that `grid.circles`

daubs `grid.polygon`

with white. You can solved it by changing order of `grid.circle`

and `grid.polygon`

(or add `gp = gpar(fill=NA)`

to `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")))
```