2

I have a boxplot for 39 species and the annual temperature values of their distribution range (bio01). The median values are in row 3 of the boxplot$stats. I want to color the highest median value red and the lowest blue, i.e. a temperature gradient. The code below for medcol is not working. Can somebody suggest a solution?

b <- boxplot(bio01 ~ species, data=species.threshold.22.climate.751, las=2, ylab ="bio01", par(mar = c(18, 5,5, 2)+ 0.1), cex.names=0.2, bw = 5, medcol=b$stat[3,])
b$stats
gagaouthu
  • 299
  • 1
  • 3
  • 9
Niels Raes
  • 51
  • 1
  • 7

2 Answers2

3

This should see you on the right track:

# Dummy data
x = data.frame(col1=rep(month.abb, 20), col2=rnorm(20*12))

# develop colour gradient
cols = colorRampPalette(c("blue", "red"))
# Calculate medians
m = unname(tapply(x$col2, x$col1, median))

# Plot and colour by size of median
boxplot(col2 ~ col1, x, col=cols(length(unique(x$col1)))[findInterval(m, sort(m))])

And the output: enter image description here

With thanks to One colour gradient according to value in column scatterplot in R for the interval help.

Community
  • 1
  • 1
MikeRSpencer
  • 1,276
  • 10
  • 24
0

You are trying to pass b$stats[,3] to boxplot, which itself creates the variable b. This won't work.

The simplest thing is maybe to call boxplot twice. Here's an example that works:

b <- boxplot(weight ~ Diet, data=ChickWeight)
boxplot(weight ~ Diet, data=ChickWeight, medcol=order(b$stats[,3]))

or you can do something more complex like

b <- boxplot(weight ~ Diet, data=ChickWeight)
colors <- colorRampPalette(c("red", "blue"))
colors <- colors(nrow(b$stats))
boxplot(weight ~ Diet, data=ChickWeight, medcol=colors[order(b$stats[,3])])