R provides a rich environment for cartographic plotting.

Maps with ggplot2

The powerful ggplot2 library is capable of producing quite nice looking maps. In order to showcase these capacities we will visualize the location of weather stations maintained by Deutscher Wetterdienst (DWD) (German Weather Service). The data was downloaded from the DWD data portal on April 21, 2017.

First, before plotting any data we have to prepare our data set. Hence, we start by importing the data set and assign it a proper name. In this example we use the read.csv2() function for loading the data.

dwd <- read.csv2("https://userpage.fu-berlin.de/soga/300/30100_data_sets/DWD.csv",
                 stringsAsFactors = FALSE)

The dwd data set consists of 599 rows, each of them representing a particular weather station in Germany, and 22 columns, each of them corresponding to a variable/feature related to that particular weather station. For the purpose of this tutorial we are only interested in the variables LAT and LON, corresponding to the geographic coordinates of each particular weather station in the data set, as well as the record length in years (RECORD.LENGTH). Hence, we subset or data set based on these variables. Further, we make sure that we exclude all missing values.

## subset data
dwd <- dwd[, c('LAT','LON', "RECORD.LENGTH")]
## drop missing values
dwd <- dwd[complete.cases(dwd), ]
## [1] 599
## 1 47.8413  8.8493            55
## 2 50.7827  6.0941           160
## 3 52.9335  8.2370            45
## 4 48.2156  8.9784            30
## 5 48.6159 13.0506            64
## 6 52.4853  7.9126            55

For the purpose of visualization we categorize the variable RECORD.LENGTH into five groups:

To achieve this task we apply the mutate() function provided by the dplyr package. If you get an error message, type install.packages('dplyr') to install the package.

dwd <- dwd %>% 
                                      breaks = c(-Inf, 10, 30, 60, 90, Inf), 
                                      labels = c("very short (<10)", 
                                                 "short (10-30)",
                                                 "middle (30-60)",
                                                 "long (60-90)", 
                                                 "very long (>90)")))

Now we are ready to plot a map to visualize the spatial distribution of the DWD weather stations. Therefore we rely on the raster package and on the ggplot2 package.


If you get an error message saying that one or both of the packages are not found, type install.packages(c('raster', 'ggplot2')) to install the packages.

Basically we use only the getData() function from the raster package. This function provides us with getting geographic data for Germany. Type help(getData) into your R console for further details.

# Retrieve Federal States by the the getData() function from the raster package
germany <- getData(country = "Germany", level = 1) 

In the next step we build a ggplot2() graph layer by layer.

ggplot() +
  geom_polygon(data = germany,
               aes(x = long, y = lat, group = group),
               colour = "grey10", fill = "#fff7bc") +
  geom_point(data = dwd,
             aes(x = LON, y = LAT, col = RECORD.LENGTH.CATEGORY),
             alpha = .5,
             size = 1.5) +
  coord_map() +
  theme_bw() +
  xlab("Longitude") + ylab("Latitude") +
  labs(color = 'Record length') + 
  ggtitle('DWD Weather Stations')