Dr. Mark Gardener

 
About

Community Ecology Cover

Community Ecology: Analytical Methods Using R and Excel


Writer's Bloc

On this page you can find out about my latest writing project. I'll post updates on progress, tables of contents and also some of the R scripts (and possibly Excel spreadsheets) I am developing in support of the new book. I'll try to keep the material reasonably up to date.

The Writer's Bloc homepage contains a table of contents and an index of the pages that contain custom R commands and R scripts.


Community Ecology: analytical Methods Using R and Excel

Available now from
Pelagic Publishing

Pelagic Publishing

 

Chapter 8. Diversity – Indices

Visualizing entropy profiles for several samples in one plot window

function: entropy_plot()

Top

Chapter 8. Diversity – Indices

Plotting entropy across several scales, q

Some measures of diversity, or more properly entropy, operate at different scales (q). At low values of q the rarest species are given more "priority" and as q increases so the rare species are given less "weight". The vegan package can calculate Rényi and Tsallis entropies and can also plot the results via the lattice package. However, it shows results in separate panes, one for each sample. I thought it was useful to be able to plot the results in a single pane. So, I wrote a script that takes the result of a Rényi or Tsallis entropy calculation and produces a plot using the matplot() command.

The staring point is the result of an entropy calculation, for example:

> gb.H <- tsallis(gbt)

> gb.H <- renyi(gbt, scales = c(0,1,2,Inf))

Once you have a result you can use the script that follows – it makes a function called entropy_plot(). You can add a legend, set the colours, plotting characters and line type as well as others.

## Compare diversity graphically
## Mark Gardener 2012
## www.dataanalytics.org.uk.uk
entropy_plot <- function(H, type = "l", ylab = "Diversity", xlab = "Scale",
                         legend = TRUE, col = palette(),
                         lty = 1:6, cex = 1,
pch = NULL, cex.legend = 1,...) { # start function
 #          H = result of entropy calculation (Renyi or Tsallis)
 #       type = the plot type, lines, points, both
# cex.legend = expansion for legend # ylab = y-axis label # xlab = x-axis label # legend = add a legend or not # ... = methods to pass to matplot()
## Mark Gardener 2012
## www.dataanalytics.org.uk
## Check data

    if(inherits(H, "renyi") == FALSE)
  stop("Data must be an entropy result", call. = FALSE)
## Calculations

  entropy <- t(H)              # rotate original result object to make matrix
        q <- rownames(entropy) # values to use for x-axis (the scales)
## Plot results without axes

  matplot(q = NULL, entropy, type=type, pch=pch, axes = FALSE,
          col = col, lty = lty, cex = cex, ylab=ylab, xlab=xlab, ...)
## Add axes

 axis(2)
 axis(1, at = 1:length(q), labels = q)
  box()
## Make legend

     if(legend == TRUE) { # start legend function
 legend("topright", legend = colnames(entropy),
        col=col, lty=lty, cex=cex.legend, pch=pch, bty="n")
 } # end of legend
} # end of main function
## END

By default the legend is added and placed top-right – this is usually the best spot as the entropy profiles tend to leave that area alone. The default plot type is lines only, type = "l", so if you want to see the points too then use type = "b" and set pch to something sensible. For example:

> entropy_plot(gb.H, cex = 2, type = "b", pch = 1:3, cex.legend = 1.5, ylab = "Rényi entropy")

Entropy Plot
Comparing Rényi entropy

Produces a plot like the one above (for my data!).


Top

Providing training for:

  • Ecology
  • Data analysis
  • Statistics
  • R The statistical programming language
  • Data management
  • Data mining

My Publications

Managing Data Using Excel, Cover

See my personal pages at GardenersOwn

Follow me...
Facebook Twitter Google+ Linkedin Amazon
Top
Courses
Publications
Contact DataAnalytics Homepage