How much of the world is woody?

This notebook was generated by the Data/Processing notebook, and runs on an environment generated therein. Most plots converted to Plotly for interactivity.

This is the full analysis that underlies our paper.

knitr::opts_chunk$set(tidy=FALSE)
invisible(suppressWarnings(sample(1:250, 1, pr=rep(1, 250), replace=TRUE)))

Preliminaries

Ensure that you have all the required packages installed by running

make deps

Before running this script, be sure to have the required data in place. If you have make installed, this can be done by running

make data-processed

at the command line. See README.md for more details.

See the file data/README.md for more information about the data that we use, and alternative ways of running things if you don't have make installed.

library(diversitree)
source("R/load.R")
source("R/util.R")

Colours used throughout:

cols.methods <- c(binomial="#a63813",
                  hypergeometric="#4d697f")
cols.tree <- c(Monilophytes="#a63813",
               Gymnosperms="#21313b",
               BasalAngiosperms="#eeb911",
               Monocots="#204d14",
               Eudicots="#4d697f",
               Rest="gray15")
cols.woody <- c(Woody="#533d0c",
                Herbaceous="#799321")
cols.woody <- c(Woody="black",
                Herbaceous="white")
cols.shading <- "#eeb911"
cols.tropical <- c(tropical="#ea7518",
                   temperate="#62a184")

The 'load.woodiness.data.genus' function hides a reasonable amount of data cleaning required to load the data. This mostly involves matching the woodiness data set from Zanne et al. to our species list (derived from The Plant List), cleaning up synonomies, and then collapsing down to genus.

The final object has columns

  • genus, family, order -- taxonomic information

  • W, V, H -- number of species scored as woody, variable, herbaceous (respectively)

  • N -- number of species in the genus

  • K -- number of species with known state, after dropping all "variable" species

  • p -- fraction of known species that are woody, after dropping all "variable" species

dat.g <- load.woodiness.genus()

Imputing the state of missing species

For each genus:

A. If the genus has a valid fraction of species (i.e. K > 0 so p is defined), then sample the number of species that are woody from either

  • the binomial distribution (strong prior; assuming known species were sampled with replacement from the pool of species); or

  • the hypergeometric distribution (weak prior; assuming that known species were sampled without replacement from the pool of species).

B. If the genus has no valid fraction of species (i.e., K == 0 so p is undefined), then sample from the emperical distribution of per-genus fractions. We're going to feed data into this by taxonomic order, so this will come from the per-order distribution.

sim <- function(x, nrep, with.replacement=TRUE, p=1/20) {
  ## First, focus on cases where we have a valid estimate of the
  ## fraction of species that are woody (i.e., at least one known
  ## species).
  ok <- !is.na(x$p)

  w <- matrix(NA, nrow(x), nrep)

  ## A: genera with any known species
  if (with.replacement)
    w[ok,] <- x$W[ok] + rbinom(sum(ok), x$N[ok]-x$K[ok], x$W[ok]/x$K[ok])
  else
    w[ok,] <- t(sapply(which(ok), function(i)
                       rhyper2(nrep, x$H[i], x$W[i], x$N[i])))

  ## B: genera with no known species
  n.unk <- sum(!ok)
  w[!ok,] <- apply(w[ok,,drop=FALSE] / x$N[ok], 2, function(y)
                   rbinom(n.unk, x$N[!ok], quantile(y, runif(n.unk))))
  
  rownames(w) <- x$genus

  summarise.sim(w, x[c("order", "family", "genus",
                       "W", "V", "H", "N", "K")])
}

This collects up the results at different taxonomic levels.

summarise <- function(x, p=1/20)
  structure(c(mean(x), quantile(x, c(p/2, 1-p/2))),
            names=c("mean", "lower", "upper"))
summarise.sim <- function(w, info) {
  order <- info$order[[1]]

  info.cols <- c("W", "V", "H", "N", "K")

  ## Genus is easy;
  w.g <- cbind(info, t(apply(w, 1, summarise)))

  ## Family is a pain:
  w.f <- do.call(rbind,
                 lapply(split(as.data.frame(w), info$family), colSums))
  w.f <- t(apply(w.f, 1, summarise))
  w.f <- data.frame(order=order,
                    aggregate(info[info.cols], info["family"], sum),
                    w.f, stringsAsFactors=TRUE)
  rownames(w.f) <- NULL
  
  ## Order is easy; we are guaranteed to have just one order here, so:
  w.o <- data.frame(order=order,
                    as.data.frame(t(colSums(info[info.cols]))),
                    t(summarise(colSums(w))), stringsAsFactors=FALSE)

  ret <- list(genus=w.g, family=w.f, order=w.o)
  attr(ret, "total") <- colSums(w)
  ret
}

rhyper2 <- function(nn, s0, s1, xn, fraction=FALSE) {
  x1 <- seq(s1, xn - s0)
  x0 <- xn - x1
  p1 <- dhyper(s1, x1, x0, s0+s1)
  p1 <- p1 / sum(p1)
  x1[sample(length(p1), nn, TRUE, p1)]
}

do.simulation <- function(dat.g, nrep, with.replacement) {
  f <- function(level) {
    ret <- do.call(rbind, lapply(res, "[[", level))
    rownames(ret) <- NULL
    ret[c("p.mean", "p.lower", "p.upper")] <-
      ret[c("mean", "lower", "upper")] / ret[["N"]]
    ret
  }

  res <- lapply(split(dat.g, dat.g$order),
                sim, nrep, with.replacement)
  total <- rowSums(sapply(res, attr, "total"))
  overall <- summarise(total)
  overall.p <- overall / sum(dat.g$N)

  list(genus=f("genus"), family=f("family"), order=f("order"),
       overall=overall, overall.p=overall.p, total=total)
}
set.seed(1)
if (!(exists("res.b") && exists("res.h"))) {
  res.b <- do.simulation(dat.g, 1000, TRUE)  # binomial - with replacement
  res.h <- do.simulation(dat.g, 1000, FALSE) # hypergeometric - without
}

Repeat this sampling proceedure under a more extreme classification; all variable species scored as woody:

if (!(exists("res.b.w") && exists("res.h.w"))) {
  dat.g.w <- load.woodiness.genus(extreme="woody")
  res.b.w <- do.simulation(dat.g.w, 1000, TRUE)  # binomial       & woody
  res.h.w <- do.simulation(dat.g.w, 1000, FALSE) # hypergeometric & woody
}

...and with all variable species scored as herbaceous:

if (!(exists("res.b.h") && exists("res.h.h"))) {
  dat.g.h <- load.woodiness.genus(extreme="herbaceous")
  res.b.h <- do.simulation(dat.g.h, 1000, TRUE)  # binomial       & herby
  res.h.h <- do.simulation(dat.g.h, 1000, FALSE) # hypergeometric & herby
}

Distribution of estimates of woodiness

This is the raw distribution; i.e., our estimate of the fraction of species that are woody and its estimate.

suppressMessages(library(dplyr, quietly=TRUE))
suppressMessages(library(plotly, quietly=TRUE))

fig.distribution.raw <- function(res.b, res.h) {
  n.spp <- sum(res.b$order$N)
  p.b <- res.b$total / n.spp * 100
  p.h <- res.h$total / n.spp * 100
  
  r <- range(p.b, p.h)
  br <- seq(r[1], r[2], length.out=30)

  h.b <- hist(p.b, br, plot=FALSE)
  h.h <- hist(p.h, br, plot=FALSE)
  
  hg.b <- data.frame(c(h.b[c("mids","density")]))
  hg.h <- data.frame(c(h.h[c("mids","density")]))

  xlim <- c(42, 50)
  ylim <- range(h.b$density, h.h$density)

  cols <- cols.methods
  
  plot_ly(opacity = 0.6) %>%
  add_bars(data = hg.b, x = ~mids, y = ~density, 
    name = "binomial w/replacement",
    marker = list(color = cols[1],
                  line=list(color='black',width=1))) %>%
  add_bars(data = hg.h, x = ~mids, y = ~density,
    name = "hypergeometric w/o replacement",
    marker = list(color = cols[2],
                  line=list(color='black',width=1))) %>%
  layout(barmode = "overlay", 
         legend = list(x = 0.05, y = 1),
         yaxis = list(title = "Probability density",range=ylim),
         xaxis = list(title = "Percentage of woody species among all vascular plants",range=xlim)
  )

}
fig.distribution.raw(res.b, res.h)

Next, expand that plot to include the more extreme estimates (variable species as woody and variable species as herbaceous). This shows how much the classification errors affect the analysis.

fig.distribution.raw.errors <- function(res.b,   res.h,
                                        res.b.w, res.h.w,
                                        res.b.h, res.h.h) {
  n.spp <- sum(res.b$order$N)
  res <- list(b  =res.b,   h  =res.h,
              b.w=res.b.w, h.w=res.h.w,
              b.h=res.b.h, h.h=res.h.h)
  p <- lapply(res, function(x) x$total / n.spp * 100)

  r <- range(unlist(p))
  br <- seq(r[1], r[2], length.out=30)

  h <- lapply(p, hist, br, plot=FALSE)

  xlim <- c(42, 50)
  ylim <- range(unlist(lapply(h, function(x) x$density)))

  hdf <- lapply(h, function(x) {
    foo = data.frame(c(x[c("mids","density")]))
    foo$widths = diff(x[["breaks"]])
    foo
  })

  cols <- cols.methods

  p1 <- plot_ly() %>% 
    add_bars(data = hdf$b.w, x = ~mids, y = ~density, width = ~widths,
      marker=list(color=cols[[1]]),opacity = .5) %>%
    add_bars(data = hdf$b.h, x = ~mids, y = ~density, width = ~widths,
      marker=list(color=cols[[1]]),opacity = .5) %>%
    add_bars(data = hdf$b, x = ~mids, y = ~density, width = ~widths,
      marker=list(color=cols[[1]],opacity=.7,
                  line=list(color='black',width=1))) %>%
    layout(showlegend=FALSE,
      xaxis=list(range=xlim,title=NA,showticklabels=FALSE),
      yaxis=list(range=ylim))

  p2 <- plot_ly() %>% 
    add_bars(data = hdf$h.w, x = ~mids, y = ~density, width = ~widths,
      marker=list(color=cols[[2]]),opacity = .5) %>%
    add_bars(data = hdf$h.h, x = ~mids, y = ~density, width = ~widths,
      marker=list(color=cols[[2]]),opacity = .5) %>%
    add_bars(data = hdf$h, x = ~mids, y = ~density, width = ~widths,
      marker=list(color=cols[[2]],opacity=.7,
                  line=list(color='black',width=1))) %>%
    layout(showlegend=FALSE,
      xaxis=list(range=xlim),
      yaxis=list(range=ylim))
    
  subplot(p1,p2,nrows=2) %>% # subplots need axis titles set here
    layout(xaxis2=list(title="Percentage of woody species among all vascular plants"),
           yaxis=list(title="Probability density"),
           yaxis2=list(title="Probability density"))
}
fig.distribution.raw.errors(res.b, res.h, res.b.w, res.h.w, res.b.h, res.h.h)

This draws the upper boundary of a bar chart in Plotly: x = breaks, y = heights.

add_bline <- function(p, x = NULL, y = NULL, ...,
                      data = NULL, inherit = TRUE) {
    
  xvert <- c(rbind(x,x))
  yvert <- c(0,rbind(y,y),0)
  
  add_trace(p, x = ~xvert, y = ~yvert, type = "scatter", mode = "lines",
            ..., data = data, inherit = inherit)
}

Woodiness is structured among genera and other taxonomic groups. Make a plot of per genus/family/order estimates of woodiness:

fig.fraction.by.group <- function(res.b, res.h, dat.g, level="genus") {
  op <- par(mfrow=c(2, 1), mar=c(2, 2, .5, .5), oma=c(2, 0, 0, 0))
  on.exit(par(op))
  lwd <- 1.5

  n.br <- c(genus=50, family=40, order=30)[[level]]
  tmp <- aggregate(dat.g[c("W", "K")], dat.g[level], sum)
  tmp <- tmp[tmp$K >= 10,] # at least 10 records per group
  h <- hist(100 * tmp$W / tmp$K, n.br, plot=FALSE)

  hdf <- data.frame(c(h[c("mids","density")]))
             
	p0 <- plot_ly() %>%
    add_bline(x = h[["breaks"]], y = h[["density"]],color=I('black'),
              showlegend=FALSE) %>%
    layout(
      xaxis=list(range=c(-2,102),zeroline=FALSE),
      yaxis=list(showticklabels=FALSE))
  
  cols <- cols.methods

  x.b <- res.b[[level]]
  x.h <- res.h[[level]]
  h.b <- hist(100*x.b$p.mean[x.b$N >= 10], n=n.br, plot=FALSE)
  h.h <- hist(100*x.h$p.mean[x.h$N >= 10], n=n.br, plot=FALSE)
  ylim <- range(h.b$density, h.h$density)
  
  p1 <- plot_ly() %>%
    add_bline(x = h.b[["breaks"]], y = h.b[["density"]],
      color=I(cols[[1]]),name="Strong prior (binomial)") %>%
    add_bline(x = h.h[["breaks"]], y = h.h[["density"]],
      color=I(cols[[2]]),name="Weak prior (hypergeometric)") %>%
    layout(xaxis=list(range=c(-2,102),zeroline=FALSE),
           yaxis=list(showticklabels=FALSE,title=NA))
  
  subplot(p0, p1, nrows = 2) %>%
    layout(legend = list(x = 0.1, y = 0.40),
           xaxis2=list(title="Percentage of woody species in genus"),
            yaxis=list(title="Probability density"),
           yaxis2=list(title="Probability density"))
}
fig.fraction.by.group(res.b, res.h, dat.g, "genus")
fig.fraction.by.group(res.b, res.h, dat.g, "family")
fig.fraction.by.group(res.b, res.h, dat.g, "order")
0 items

Woodiness also varies over the tree; plot the per-order estimate of woodiness around the tree (the code to do this is not very pretty, so is kept separately in R/plot-tree.R)

source("R/plot-tree.R")

Phylogeny at the level of order:

phy.o <- load.phylogeny.order()

An option tweak is needed to get a little more height for a nice-looking plot.

options(nextjournal.ggplot.device.default.height=6)

fig.fraction.on.phylogeny(phy.o, res.b)
fig.fraction.on.phylogeny(phy.o, res.h)

Reset plot height.

options(nextjournal.ggplot.device.default.height=4.471428)

Survey:

Raw results of the survey:

d.survey <- load.survey()

Convert estimates to normal using logit transformation:

d.survey$Estimate.logit <- boot::logit(d.survey$Estimate / 100)

Model with training and familiarity as factors:

res <- lm(Estimate.logit ~ Training + Familiarity, d.survey)
summary(res)
anova(res)
0 items

Regression against |latitude|:

res.lat <- lm(Estimate.logit ~ abs(Lat), d.survey)
anova(res.lat)
summary(res.lat)
0 items

Here is the fitted result:

s.res <- data.frame(
  fitted(res.lat),
  residuals(res.lat),
  res.lat$model["abs(Lat)"]
)
colnames(s.res) <- c("fit","res","lat")

plot_ly(s.res, type='scatter',
        x = ~lat, y = ~res, name='Data',mode='markers') %>%
  add_trace(x =~ lat, y = ~fit, name='Fit',mode='lines') %>%
  layout(xaxis=list(title="abs(Lat)"),
         yaxis=list(title="Estimate.logit"),
         legend = list(orientation = 'h'))

As a categorical tropical/non-tropical variable:

res.tro <- lm(Estimate.logit ~ Tropical, d.survey)
anova(res.tro)
summary(res.tro)
0 items

Distribution of estimates vs different levels of botanical familiarity and education, with the estimates from the database overlaid.

fig.survey.results <- function(fxn, cats, d.survey, res.b, res.h, ...) {
  ci <- 100*cbind(res.b$overall.p, res.h$overall.p)
  col <- cols.methods
  
  frame = model.frame(fxn, d.survey)
  
  # For some reason add_ribbons() needs this number of data points
  # despite being on a separate x axis.
  ndat = nrow(frame) 

  plot_ly(frame) %>%
    add_boxplot(y = ~Estimate, x = cats, 
      name="Survey Responses",legendgroup="group1") %>%
    add_lines(x = ~c(0,1), y = ~c(ci["mean",1],ci["mean",1]),
      xaxis="x2",line=list(color='black',width=0.5),
      name="Binomial Mean",legendgroup="group2") %>%
    add_polygons(xaxis="x2",
      x=~c(0,0,1,1), name="Binomial Range",legendgroup="group2",
      y=~c(ci["lower",1],ci["upper",1],ci["upper",1],ci["lower",1]),
      fillcolor=col[1],line=list(color=col[1]),opacity=0.5) %>%
    add_lines(x = ~c(0,1), y = ~c(ci["mean",2],ci["mean",2]),
      xaxis="x2", line=list(color='black',width=0.5),
      name="Hypergeometric Mean",legendgroup="group3") %>%
    add_polygons(xaxis="x2",
      x=~c(0,0,1,1), name="Hypergeometric Range",legendgroup="group3",
      y=~c(ci["lower",2],ci["upper",2],ci["upper",2],ci["lower",2]),
      fillcolor=col[2],line=list(color=col[2]),opacity=0.5) %>%
    layout(
      yaxis=list(title="Estimate of percentage woodiness",
        zeroline=FALSE,showline=TRUE,range=c(0,100)),categoryorder="array",
                              categoryarray=c(1,2,3,4,5),
                              ticktext=c("1","2","3","4","5"),
      xaxis=list(title=NA,showline=TRUE),
      xaxis2=list(overlaying = "x",showticklabels=FALSE),
      ...
    )
}
fig.survey.results(Estimate ~ Familiarity, ~Familiarity, d.survey, res.b, res.h)

# rename training levels for display, filter NA
library(forcats)
lels <- levels(d.survey$Training)
sh_training <- fct_recode(d.survey$Training[!is.na(d.survey$Training)],
  "Postgrad"=lels[1], "Part postgrad"=lels[2], "Undergrad"=lels[3],
  "Part undergrad"=lels[4], "None"=lels[5])

fig.survey.results(Estimate ~ Training, sh_training, d.survey, res.b, res.h)

And the raw distribution of survey results, showing the general tendency for relatively low estimates, again overlaid with the estimates from the database:

fig.survey.distribution <- function(d.survey, res.b, res.h) {
  lwd <- 1.5
  col <- cols.methods

  ci <- 100*cbind(res.b$overall.p, res.h$overall.p)
  
  hist.full <- hist(d.survey$Estimate, plot=FALSE)
  hist.trop <- hist(d.survey$Estimate[d.survey$Tropical],
                    breaks=seq(0,90,10),plot=FALSE)
  hist.temp <- hist(d.survey$Estimate[!d.survey$Tropical],
                    breaks=seq(0,90,10),plot=FALSE)
  hist.data <- data.frame(c(
    hist.full[c("mids","counts")],
    hist.trop[c("mids","counts")],
    hist.temp[c("mids","counts")]
  ))
  colnames(hist.data) <- c(
    "mids","counts","mids.trop","counts.trop","mids.temp","counts.temp"
  )
  ndat = nrow(hist.data) 
  
  plot.top = 10*ceiling(max(hist.full$counts)/10)
  

  plot_ly(hist.data) %>%
    add_bars(x = ~mids, y = ~counts, type = 'bar',name="Total Counts",
      width = 10,marker = list(line = list(color = 'black',width=1)),
            legendgroup="group1") %>%
    add_polygons(name="Binomial Range",
      x = ~c(ci["lower",1],ci["lower",1],ci["upper",1],ci["upper",1]),
      y = ~c(0,plot.top,plot.top,0),legendgroup="group2",
      color=I(col[1]),opacity=0.5,line=list(width=0)) %>%
    add_lines(y = ~c(0,plot.top), x = ~c(ci["mean",1],ci["mean",1]),
      mode="lines",marker = list(opacity=0), name = "Binomial Mean",
      line=list(color='black',width=0.5),legendgroup="group2") %>%
    add_polygons(name="Hypergeometric Range",
      x = ~c(ci["lower",2],ci["lower",2],ci["upper",2],ci["upper",2]),
      y = ~c(0,plot.top,plot.top,0),legendgroup="group3",
      color=I(col[2]),opacity=0.5,line=list(width=0)) %>%
    add_lines(y = ~c(0,plot.top), x = ~c(ci["mean",2],ci["mean",2]),
      mode="lines",marker = list(opacity=0), name = "Hypergeometric Mean",
      line=list(color='black',width=0.5),legendgroup="group3") %>%
    add_bline(x = hist.trop[["breaks"]], y = hist.trop[["counts"]],
      name="Tropical Counts", color=I(cols.tropical[1]),
      legendgroup="group1") %>%
    add_bline(x = hist.temp[["breaks"]], y = hist.temp[["counts"]],
      name="Temperate Counts", color=I(cols.tropical[2]),
      legendgroup="group1") %>%
    layout(barmode='stack',
      xaxis=list(title="Estimate of percentage woodiness",
        zeroline=FALSE,showline=TRUE,range=c(0,100)),
      yaxis=list(title="Number of responses",
        showline=TRUE, range=c(0,plot.top)),
      legend = list(x = 0.65, y = 1)
    )
}
fig.survey.distribution(d.survey, res.b, res.h)

Variability of a genus vs size

These plots look at the how variable a genus is vs its size (left column) or the number of species in a known state (right column). The top row looks at how variable the genus is (a single type or a mixed type), the middle row at the probability that a genus is varaible. The bottom row looks at the proportion of species that are woody in a genus vs its size, testing if woody genera are relatively larg or relatively small (woody genera are relatively smaller).

fig.variability <- function(dat.g) {
  dat.g$p.rare <- (0.5 - abs(dat.g$p - 1/2)) * 2
  dat.g$variable <- dat.g$p.rare > 0

  sub <- dat.g[!is.nan(dat.g$p),]

  ## Breaks for the moving average:
  br.N <- log.seq.range(sub$N, 20)
  br.K <- log.seq.range(sub$K, 15)

  ## Classify points:
  i.N <- findInterval(sub$N, br.N, all.inside=TRUE)  
  i.K <- findInterval(sub$K, br.K, all.inside=TRUE)  

  ## Midpoints for plotting.
  mid.N <- (br.N[-1] + br.N[-length(br.N)])/2
  mid.K <- (br.K[-1] + br.K[-length(br.K)])/2

  m.N <- tapply(sub$p.rare, i.N, mean)
  m.K <- tapply(sub$p.rare, i.K, mean)
  p.N <- tapply(sub$variable, i.N, mean)
  p.K <- tapply(sub$variable, i.K, mean)
  f.N <- tapply(sub$p, i.N, mean)
  f.K <- tapply(sub$p, i.K, mean)

  pch <- 19
  cex <- 0.5
  col <- "#00000066"

  op <- par(oma=c(4.1, 4.1, 0, 0),
            mar=c(1.1, 1.1, .5, .5),
            mfrow=c(3, 2))
  
  plot(p.rare ~ N, sub, pch=pch, cex=cex, col=col, log="x",
       axes=FALSE)
  lines(m.N ~ mid.N, col="red")
  axis(1, labels=FALSE) 
  axis(2, c(0, 1), c("Single type", "50:50"), las=1)
  mtext("Variability", 2, 3)
  box(bty="l")
  label(.02, .96, 1)

  plot(p.rare ~ K, sub, pch=pch, cex=cex, col=col, log="x",
       axes=FALSE)
  lines(m.K ~ mid.K, col="red")
  axis(1, labels=FALSE) 
  axis(2, c(0, 1), labels=FALSE)
  box(bty="l")
  label(.02, .96, 2)

  plot(variable ~ N, sub, pch=pch, cex=cex, col=col, log="x",
       bty="l", las=1, axes=FALSE)
  lines(p.N ~ mid.N, col="red")
  axis(1, labels=FALSE)
  axis(2, las=1)
  mtext("Probability genus is variable", 2, 3) 
  box(bty="l") 
  label(.02, .96, 3)

  plot(variable ~ K, sub, pch=pch, cex=cex, col=col, log="x",
       bty="l", yaxt="n", las=1, axes=FALSE)
  lines(p.K ~ mid.K, col="red")
  axis(1, labels=FALSE)
  axis(2, labels=FALSE) 
  box(bty="l") 
  label(.02, .96, 4)

  plot(p ~ N, sub, pch=pch, cex=cex, col=col, log="x",
       bty="l", las=1)
  lines(f.N ~ mid.N, col="red")
  mtext("Number of species in genus", 1, 3)
  mtext("Proportion of species woody", 2, 3)
  label(.02, .96, 5)

  plot(p ~ K, sub, pch=pch, cex=cex, col=col, log="x",
       bty="l", yaxt="n", las=1)
  lines(f.K ~ mid.K, col="red")
  axis(2, labels=FALSE)
  mtext("Number of species with known state", 1, 3)
  label(.02, .96, 6)
}

This grid needs twice as much height to come out readable.

options(nextjournal.ggplot.device.default.height=4.471428*2)

fig.variability(dat.g)

Reset default height.

options(nextjournal.ggplot.device.default.height=4.471428)

Write out partly processed data sets

To save having to rerun everything, these are the estimates by genus, family and order for the two different sampling approaches and the three different treatment of variable species (so 3 x 2 x 3 = 18 files)

Note: locked on Nextjournal, since we don't need these with checkpoints enabled.

write.output <- function(d, type) {
  for (tax in c("genus", "family", "order"))
    write.csv(d[[tax]],
              file.path("output/results", sprintf("%s-%s.csv", tax, type)),
              row.names=FALSE)
}

Core data sets:

dir.create("output/results", FALSE)
write.output(res.b, "binomial-strong-prior")
write.output(res.h, "hypergeometric-weak-prior")

Extra data sets:

write.output(res.b.w, "binomial-strong-prior-wood-biased")
write.output(res.h.w, "hypergeometric-weak-prior-wood-biased")
write.output(res.b.h, "binomial-strong-prior-herb-biased")
write.output(res.h.h, "hypergeometric-weak-prior-herb-biased")

Meta data for interpreting these files.

metadata <- 
  list(order="Taxonomic order",
       family="Taxonomic family",
       genus="Taxonomic genus",
       W="Number of species known to be woody",
       V="Number of species known to be variable",
       H="Number of species known to be herbaceous",
       N="Estimate of the number of species in the genus/family/order",
       K="Number of species with known state (W + H)",
       mean="Mean estimated number of woody species",
       lower="0.025 quantile of estimated number of woody species",
       upper="0.975 quantile of estimated number of woody species",
       p.mean="Mean estimated fration of woody species",
       p.lower="0.025 quantile of estimated fraction of woody species",
       p.upper="0.975 quantile of estimated fraction of woody species")
metadata <- data.frame(column=names(metadata),
                       description=unlist(metadata),
                       stringsAsFactors=FALSE)
write.csv(metadata, "output/results/metadata.csv", row.names=FALSE)

Graphical abstract:

fig.graphical.abstract <- function(res.b, res.h, dat.g, d.survey) {
  p.raw <- sum(dat.g$W) / sum(dat.g$K)
  p.survey <- mean(d.survey$Estimate) / 100
  p.data <- mean(c(res.b$overall.p[["mean"]], res.h$overall.p[["mean"]]))
  
  f <- function(p, title) {
    pie(c(p, 1-p), c("Woody", "Herbaceous"), col=cols.woody)
    text(0, par("usr")[2], title, adj=c(0.5, 0), cex=1.2)
  }
  par(mfrow=c(1, 3), mar=rep(1, 4))
  f(p.raw,    "Raw data")
  f(p.survey, "Survey estimate")
  text(0, 1.5, "How much of the world is woody?", cex=1.5, xpd=NA)
  f(p.data,   "Bias corrected")
}
fig.graphical.abstract(res.b, res.h, dat.g, d.survey)

Produce PDF versions of figures for publication:

if (interactive()) {
  to.pdf("/results/fraction-by-genus.pdf", 6, 6,
         fig.fraction.by.group(res.b, res.h, dat.g, "genus"))
  to.pdf("/results/fraction-by-family.pdf", 6, 6,
         fig.fraction.by.group(res.b, res.h, dat.g, "family"))
  to.pdf("/results/fraction-by-order.pdf", 6, 6,
         fig.fraction.by.group(res.b, res.h, dat.g, "order"))

  to.pdf("/results/fraction-on-phylogeny.pdf", 6, 6,
         fig.fraction.on.phylogeny(phy.o, res.b))

  to.pdf("/results/fraction-on-phylogeny-supp.pdf", 6, 6,
         fig.fraction.on.phylogeny(phy.o, res.h))

  to.pdf("/results/distribution-raw.pdf", 6, 4,
         fig.distribution.raw(res.b, res.h))
  to.pdf("/results/distribution-raw-errors.pdf", 6, 4,
         fig.distribution.raw.errors(res.b, res.h, res.b.w, res.h.w,
                                     res.b.h, res.h.h))

  to.pdf("/results/survey-results.pdf", 6, 4,
         fig.survey.results(d.survey, res.b, res.h))

  to.pdf("/results/survey-distribution.pdf", 6, 5,
         fig.survey.distribution(d.survey, res.b, res.h))

  to.pdf("/results/variability.pdf", 7, 8,
         fig.variability(dat.g))

  to.pdf("/results/graphical-abstract.pdf", 7, 3.5,
         fig.graphical.abstract(res.b, res.h, dat.g, d.survey))
}

Version information:

sessionInfo()