Jason Fisher blog
The knitr package provides an easy way to embed R code in a Jekyll-Bootstrap blog post. The only required input is an R Markdown source file. The name of the source file used to generate this post is 2012-07-03-knitr-jekyll.Rmd, available here. Steps taken to build this post are as follows:
Create a Jekyll-Boostrap blog if you don’t already have one. A brief tutorial on building this blog is available here.
Open the R Console and process the source file:
Move the resulting image folder 2012-07-03-knitr-jekyll and Markdown file 2012-07-03-knitr-jekyll.md to the local jfisher-usgs.github.com git repository. The KnitPost function assumes that the image folder will be placed in a figs folder located at the root of the repository.
Add the following CSS code to the /assets/themes/twitter-2.0/css/bootstrap.min.css file to center images:
Here are a few examples of embedding R code:
Figure 1: Caption
Figure 2: Caption
And don’t forget your session information for proper reproducible research.
I’d like to introduce you to the Grid2Polygons function; an R function for converting sp spatial objects from class SpatialGridDataFrame to SpatialPolygonsDataFrame. The significance of this conversion is that spatial polygons can be transformed to a different projection or datum with the spTransform function in package rgdal. Postscript files created with spatial polygons are reduced in size and result in a much “cleaner” version of your image. Disadvantages of the conversion include long computational times and irreversible leveling, partitioning the range of z values. A general explanation of the algorithm is provided here; inspiration provided here.
See help documentation for argument descriptions:
The following examples highlight the functions usefulness:
Construct a simple spatial grid data frame.
m <- 5 n <- 6 z <- c(1.1, 1.5, 4.2, 4.1, 4.3, 4.7, 1.2, 1.4, 4.8, 4.8, NA, 4.1, 1.7, 4.2, 1.4, 4.8, 4.0, 4.4, 1.1, 1.3, 1.2, 4.8, 1.6, NA, 3.3, 2.9, NA, 4.1, 1.0, 4.0) x <- rep(0:6, m + 1) y <- rep(0:5, each = n + 1) xc <- c(rep(seq(0.5, 5.5, by = 1), m)) yc <- rep(rev(seq(0.5, 4.5, by = 1)), each = n) grd <- data.frame(z = z, xc = xc, yc = yc) coordinates(grd) <- ~ xc + yc gridded(grd) <- TRUE grd <- as(grd, "SpatialGridDataFrame")
Plot the grid using a gray scale to indicate values of z (fig. 1).
image(grd, col = gray.colors(30), axes = TRUE) grid(col = "black", lty = 1) points(x = x, y = y, pch = 16) text(cbind(xc, yc), labels = z) text(cbind(x = x + 0.1, y = rev(y + 0.1)), labels = 1:42, cex = 0.6)
Figure 1: Simple spatial grid data frame.
Convert the grid to spatial polygons and overlay in plot (fig. 2). Leveling is specified with cut locations at 1, 2, 3, 4, and 5, and z-values set equal to the midpoint between breakpoints. A “winding rule” is used to determine if a polygon ring is filled (island) or is a hole in another polygon.
plys <- Grid2Polygons(grd, level = TRUE, at = 1:5) cols <- rainbow(4, alpha = 0.3) plot(plys, col = cols, add = TRUE) x <- rep(0:6, m + 1) y <- rep(0:5, each = n + 1) legend("top", legend = plys[], fill = cols, bty = "n", xpd = TRUE, inset = c(0, -0.1), ncol = 4)
Figure 2: Simple gridded data represented with spatial polygons.
Apply the conversion function to the meuse data set, included in the sp package. The effect of leveling is shown in figure 3.
data(meuse.grid) coordinates(meuse.grid) <- ~ x + y gridded(meuse.grid) <- TRUE meuse.grid <- as(meuse.grid, "SpatialGridDataFrame") meuse.plys <- Grid2Polygons(meuse.grid, "dist", level = FALSE) op <- par(mfrow = c(1, 2), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0)) z <- meuse.plys[] col.idxs <- findInterval(z, sort(unique(na.omit(z)))) cols <- heat.colors(max(col.idxs))[col.idxs] plot(meuse.plys, col = cols) title("level = FALSE", line = -7) meuse.plys.lev <- Grid2Polygons(meuse.grid, "dist", level = TRUE) z <- meuse.plys.lev[] col.idxs <- findInterval(z, sort(unique(na.omit(z)))) cols <- heat.colors(max(col.idxs))[col.idxs] plot(meuse.plys.lev, col = cols) title("level = TRUE", line = -7) par(op)
Figure 3: Distance from river represented with spatial polygons.
Colors are mysterious business and choosing the right color or color palette for an R graphics can sometimes be a chore. To alleviate this burden, I present two functions for choosing color values.
To access these functions install the RSurvey package and its dependencies:
repo <- "http://cran.us.r-project.org" install.packages("RSurvey", repos = repo, dependencies = TRUE, type = "both")
col <- RSurvey::ChooseColor(col = "#669933")
Figure 1: A screenshot of the HSV color tool.
Choose Color Palette
The second function, choose_palette (source), calls upon a GUI for choosing a color palette in the HCL (Hue, Chroma, and Luminance) color space (fig. 2).
pal <- colorspace::choose_palette(terrain_hcl)
Figure 2: A screenshot of the HCL color palette tool.
An example graphic accompanies the GUI and may be used to evaluate the robustness of individual color schemes (fig. 3).