R + Shiny + ggplot2: Alaska/western Canada communities and climate

I have released a preliminary version of a new R Shiny web application. This app is focused on exploration of 2-km downscaled temperature and precipitation climate model output over Alaska and western Canada for historical and projected time periods. The data are monthly variables at a decadal time scale (decadal mean temperature and precipitation). The values corresponding to each community are those of the 2-km grid cell in which a community was located. Bear in mind that this app is a work in progress and should not be treated as a finalized, vetted product or dataset. More details can be found on the app About tab.

Now for the fun stuff!…


This Shiny app makes direct use of two additional R packages other than shiny and shinyIncubator, which are ggplot2 and Hmisc. If you are familiar with ggplot2, you will recognize its pervasive use immediately. There are some limitations to what it can do and I am a huge fan of base R graphics, but for the versatility and control that I wanted users of this app to have over plot formatting, writing a function that called ggplot a number of different ways depending on user inputs was the perfect solution. The user can plot points for temperature or precipitation, line plots for the former, bar plots only for the latter (it’s not like we’re working in Kelvin units, so a temperature barplot is not appropriate). There are options to overlay confidence bands, range bars (they are not “error bars”), bootstrap confidence intervals at fixed points, etc. Users can switch to Fahrenheit degrees and inches is they want to. Also, as an aside, if you add range bars and the bootstrap confidence interval box at the same time, don’t mistake their overlapping appearance for a classic boxplot. There are no boxplots in this app.

Where ggplot2 really shines is in all the options for faceting (making panel plots), consolidating, pooling and summarizing data from different groups within a plot, altering the orientation of the plot, changing options on the fly, for example with barplots, switching between dodge (grouped), stack (totals), and fill (proportions) to achieve different perspectives on the same data. I added the ability to click a button to download the currently displayed graphic as a pdf, as well as a csv file of the subset of data which go into the plot.

With this app I am still in the progress of “finding my way” toward increasingly general approaches to Shiny app development. I have begun doing more code externalization by splitting out different segments of app code into different files that are then source-d together. This was also my first time adding some external javascript, my use of select2, which was added to the app so that the user can begin to type a location in the location menu and have the results filter so as to quickly find the place they are looking for. Otherwise it would have just been a horrendously long menu to have to scroll through! I didn’t know much about the details and still would like to spiff it up a bit, but for the time being someone just said something along the lines of “dump all these select2 files in a folder in your app and it will work.” For the most part.

More information is available on the app About tab. Play around with the app and let me know what you think. I would like to add more features to it, as long as they relate well to one another and wouldn’t be better off in distinct apps. I’d also like to add more recent data, such as the Coupled Model Intercomparison Project Phase 5 (CMIP5) climate model output, which we downscale at SNAP over Alaska and Western Canada to 2-km resolution. Currently I only use CMIP3 historical and projected climate model outputs in this app. Higher temporal resolution data would be nice too, as in using the monthly data in addition to the decadal data they have been aggregated to here, but there I would be playing with fire; I don’t want the app to become data heavy. But with so much to do, there is no guarantee I will make changes to this preliminary app in the very immediate future. But when I do, I’ll make a new post with updates.

This entry was posted by Matt Leonawicz.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: