Skip to content

Instantly share code, notes, and snippets.

@ibartomeus
Created December 17, 2012 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibartomeus/4319048 to your computer and use it in GitHub Desktop.
Save ibartomeus/4319048 to your computer and use it in GitHub Desktop.
Plot for the post http://ibartomeus.wordpress.com/2012/12/17/who-are-the-pollinators. Include function diamondplot2
#who are the pollinators? (a plot)
#http://ibartomeus.wordpress.com/2012/12/17/who-are-the-pollinators
#enter data manually
d <- matrix(nrow = 6, ncol = 7)
d[,1] <- c(20.000, 10, 10, 10, 200.000, 100)
d[,2] <- c(6.000, 4, 8, 9, 100.000, 20)
d[,3] <- c(14.500, 6, 4, 9, 80.000, 4)
d[,4] <- c(30.000, 7, 2, 7, 50.000, 3)
d[,5] <- c(0.328, 10, 2, 3, 1.000, 5)
d[,6] <- c(0.053, 10, 2, 3, 0.500, 3)
d[,7] <- c(20.000, 5, 1, 6, 1.000, 11)
d <- as.data.frame(d)
rownames(d) <- c("Number of species", "Efficiency", "Visitation frquency", "Distribution", "Number of plants pollinated", "Number of crops pollinated")
colnames(d) <- c("Bees","Syrphid flies","Butterflies", "Moths", "Hummingbirds", "Bats", "Other insects")
str(d)
#x <- as.data.frame(d)
#this function is borrowed from diamondplot{plotrix} but it normalize by rows in the data.frame, that is, by axes, making each axe maximum equal to 1. Hence, axes are not comparable among them, but groups (columns) are comparable within axes. Former function normalized by columns, making comparisions among axes meaningful for a given group, but groups within axes show completely misleading rankings.
diamondplot2 <- function (x, bg = gray(0.6), col = rainbow, name = "", ...)
{
if (!is.data.frame(x))
stop("Argument has to be a data frame")
opar <- par(no.readonly = TRUE)
par(bg = bg)
prop <- 0.8
scale <- 5
deg2rad <- function(alfa) {
(alfa * pi)/180
}
#IB do not normalize the data here
#normalizza <- function(x) {
# x/max(x)
#}
coord <- function(x, prop = 17) {
n <- length(x)
alfa <- rep(NA, n)
for (i in 1:n) {
alfa[i] <- 360/n * i
}
c.x <- sin(deg2rad(alfa)) * x * prop
c.y <- cos(deg2rad(alfa)) * x * prop
list(x = c.x, y = c.y)
}
segmenti <- function(n, prop = 1, labels = NULL, scale = 5) {
plot(0, 0, axes = FALSE, xlab = "", ylab = "", main = name,...)
for (i in 1:n) {
alfa <- 360/n * i
x1 <- c(0, sin(deg2rad(alfa)) * prop)
y1 <- c(0, cos(deg2rad(alfa)) * prop)
polygon(x1, y1)
text(sin(deg2rad(alfa)), cos(deg2rad(alfa)), labels[i],
cex = 0.8)
}
for (i in 1:n) {
alfa <- 360/n * i
for (j in 1:scale) {
xa <- (sin(deg2rad(alfa)) * (1/scale) * j * prop)
ya <- (cos(deg2rad(alfa)) * (1/scale) * j * prop)
points(xa, ya, pch = 19)
et <- round((1/scale) * j * 17)
#IB: remove lab axes
#text(sin(deg2rad(0)) + 0.1, cos(deg2rad(0)) *
#(1/scale) * j * prop, et, cex = 0.8)
}
}
}
n <- dim(x)[[1]]
k <- dim(x)[[2]]
segmenti(n, prop = prop, labels = rownames(x))
xx <- x
for (j in 1:n) {
#IB normalize the data by rows
xx[j,] <- x[j, ]/max(x[j,])
}
for (j in 1:k) {
#IB uses normalized data here
polygon(coord(xx[,j], prop = prop), lty = j,
border = col(k)[j])
}
#IB make legend smaller
legend(-1.05, 1.05, legend = dimnames(x)[[2]], lty = 1:k,
col = col(k), cex = 0.6)
par(opar)
}
#diamondplot(d)
#diamondplot(as.data.frame(t(d))) #here is usefull to standardize by groups.
diamondplot2(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment