Finally, an easy way to fix the horizontal lines in ggplot2 maps

ggplot2 tries to make it super easy to add country or state borders to your map, and for the most part it works great as long as you include the entire map region in your plot (all the states or the entire world map for example). A long standing issue with the ggplot2 borders() function is when you try and zoom in a way that removes data outside of the plotting region, countries and states get cut off and not reconnected correctly, resulting in spurious horizontal lines in the plot. A forum post and ggplot2 issue both allude to this problem.

Here is a simple example showing this issue. First, a simple unprojected map that looks great because we are not trying to zoom in at all.

ggplot() +
    borders("world") + borders("state") +

Alt text

An obvious extension would be to zoom in on a region and project the map:

ggplot() +
    borders("world") + borders("state") +
    theme_bw() +
    coord_map(xlim=xlim,ylim=ylim) +

Alt text

Which pretty clearly illustrates the issue. Things look even worse if we try and add color:

ggplot() +
    borders("world",fill="darkseagreen") +
    borders("state",fill="darkseagreen") +
    theme_bw() +
    coord_map(xlim=xlim,ylim=ylim) +

Alt text

The underlying issue is that when we zoom with coord_map() the polygons get cut up, with some parts appearing on either side of the map, hence the horizontal lines when ggplot tries to connect a country or state that appears on two sides of the map. The solution to this issue is to get the raw map data then use one of the various polygon clipping libraries in R. I used PBSmapping. Here is a complete example


# plot limits
xlim = c(-165,-90)
ylim = c(10,60)

worldmap = map_data("world")
setnames(worldmap, c("X","Y","PID","POS","region","subregion"))
worldmap = clipPolys(worldmap, xlim=xlim,ylim=ylim, keepExtra=TRUE)

statemap = map_data("state")
setnames(statemap, c("X","Y","PID","POS","region","subregion"))
statemap = clipPolys(statemap, xlim=xlim,ylim=ylim, keepExtra=TRUE)

p = ggplot() +
    coord_map(xlim=xlim,ylim=ylim) +
        fill = "darkseagreen",color="grey50") +
        fill = "darkseagreen",color="grey50") +
    labs(y="",x="") +

Alt text

This could also be used to fix the fill issues when doing ggplot filled contour plots.

Who wants to maintain pgfSweave?

So the time has come for me to face the fact that I have no time to maintain pgfSweave. It was recently archived because I didn’t make necessary changes to comply with some CRAN policies. SO, I need someone to step up to the plate to make some tweakes, put it back up on CRAN and maintain it in the future. Is it you?

To do the job you need at a basic understanding (or a desire to learn) the following:

  • Git/Github
  • Sweave
  • LaTeX
  • Make
  • R (duh :p)

Compensation includes:

  • Undying glory
  • The respect of your peers
  • Self satisfaction
  • A high-five from me if we ever meet in person!

If you are interested in the job please contact me at cameron[dot]bracken[at]gmail[dot]com

Syntax highlighting of roxygen documentation in TextMate

With roxygen now on github, the release of roxygen2 and Hadley’s might now behind the project I expect roxygen to gain even more momentum.

R development in TextMate is great with the R bundle. Unfortunately the R bundle does not support highlighting of roxygen documentation by default. That was always a sticking point for me switching to roxygen because TextMate has really nice highlighting for Rd files.

So I hacked on the syntax file to get at least some preliminary highlighting of stuff after roxygen comments (#'). The file is in a gist over at github. It doesn’t yet work quite as well as I would like but it is better than nothing. In particular, highlighting of R code in examples does not work. Contributions/additions are welcome.

You must have must have the R bundle installed. The easiest way to install is to open the bundle editor (“Bundles” > “Bundle Editor” > “Show Bundle Editor”) and copy this file over the R syntax file from the R bundle. Then “Reload Bundles” or restart TextMate.

You may need to install the R bundle first if you have not already:

mkdir -p /Library/Application\ Support/TextMate/Bundles
cd /Library/Application\ Support/TextMate/Bundles
svn co

Then either reload the bundles or restart TextMate to get the bundle activated.