/mapDK

Easy maps of Denmark with R and ggplot package

Primary LanguageR

mapDK

This is a very incomplete draft of a mapDK package for making easy ggplot2 based maps of Denmark.

Currently, the package allows you to do two things:

  1. make basic maps of DK at the municipality or parish level
  2. turn these maps into (static) choropleth maps

I will make it possible to create interactive choropleth maps when R's interactive libraries such as rCharts, leaftletR, or ggvis, mature a little.

I will also add more maps with time. First on my list is probably municipalities from before the Danish municipality reform of 2005, but suggestions are welcome.

To install the package simply run

devtools::install_github("sebastianbarfort/mapDK")

To use the basic map simply run

library(mapDK)
mapDK()

plot of chunk unnamed-chunk-2

The default plots Denmark's 98 municipalities. You can plot at the parish level by adding detail = "parish" in the mapDK call

mapDK(detail = "parish")

plot of chunk unnamed-chunk-3

To create a static choropleth map I've added some test data to the package.

We can create a map by simply specifying the values and id's (as strings) and the dataset in the call to mapDK

mapDK(values = "indbrud", id = "kommune", data = test.data)

plot of chunk unnamed-chunk-4

If you don't provide names for all municipalities (or parishes), the function will throw a warning. Let's randomly remove 20 rows and plot the data again

test.data.2 = mapDK::test.data[-sample(1:nrow(mapDK::test.data), 20), ]
mapDK(values = "indbrud", id = "kommune", data = test.data.2)

plot of chunk unnamed-chunk-5

You can remove missing municipalities by changing show_missing to false and you can add a custom legend title by specifying the guide.label option

mapDK(values = "indbrud", id = "kommune", data = test.data.2, show_missing = FALSE,
  guide.label = "test label")

plot of chunk unnamed-chunk-6

You can also provide a sub option specifying what municipalities in your data you want plotted

mapDK(values = "indbrud", id = "kommune", data = test.data, sub = c("Viborg", "Esbjerg"))

plot of chunk unnamed-chunk-7

This also works for non-choropleth maps

mapDK(sub = c("Viborg", "Esbjerg"))

plot of chunk unnamed-chunk-8