QuantumPlots with ggplot2 and spatstat

October 19, 2015
By

(This article was first published on spatialRecology - r, and kindly contributed to R-bloggers)

Overview

Introduction

Lately, I was plotting a lot of spatial statistic functions, comparing them and trying to make a sense out of them.
To facilitate the procedure I wrote the following function to create a plot for the spatstat class ‘fv’, combined with ‘Quantum Plots’ from:

Esser, D. S., Leveau, J. H. J., Meyer, K. M., & Wiegand, K. (2014). Spatial scales of interactions among bacteria and between bacteria and the leaf surface. FEMS Microbiology Ecology, 91(3), fiu034. http://doi.org/10.1093/femsec/fiu034

The colored bands at the bottom of the plot highlight the spatial scales at which the summary statistics deviate from the simulation envelopes.

Function

quantumPlot <- function(x,colour=c("#d73027", "#ffffbf", "#91bfdb")){

  # load Packages
  require(ggplot2)
  require(ggthemes)

  # convert fv to dataframe
  env.data <- as.data.frame(tree.data)
  env.data <- env.data[-1,]

  # plot it
  gg_quantomPlot <- ggplot(env.data, aes(r, obs))+
                            # plot observed value
                            geom_line(colour=c("#4d4d4d"))+
                            # plot simulation envelopes
                            geom_ribbon(aes(ymin=lo,ymax=hi),alpha=0.1, colour=c("#e0e0e0")) +
                            # axes names and limits
                            ylim(min(env.data$obs)-1, max(env.data$obs)+2) +
                            xlab("Distance r (m)") +
                            ylab("summary statistic") +
                            # plot expected value, according to null model
                            geom_hline(yintercept=1, linetype = "dashed", colour=c("#999999")) +
                            # plot 'Quantums'
                            geom_rug(data=env.data[env.data$obs > env.data$hi,], sides="b", colour=colour[1])  +
                            geom_rug(data=env.data[env.data$obs < env.data$lo,], sides="b", colour=colour[2]) +
                            geom_rug(data=env.data[env.data$obs >= env.data$lo & env.data$obs <= env.data$hi,], sides="b", color=colour[3]) +
                            # make it look beautiful
                            theme_tufte()
  return(gg_quantomPlot)
}

https://gist.github.com/marcosci/e261a586f82d8575a843

Now you can give the result your envelope-fv object and plot it:

attach(redwood)
redwood.pcf <- envelope(redwood, fun=pcf)
quantumPlot(redwood.pcf)

To leave a comment for the author, please follow the link and comment on their blog: spatialRecology - r.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)