Child pages
  • Plotting backgroundmaps in R
Skip to end of metadata
Go to start of metadata

When plotting simple geographical data (lon,lat) in R, a background map may be useful. Some R-integrated examples are described here with different levels of detail in maps, scaling, and axis decorations. Besides 'ggplot2' and 'rworldmap', several libraries might be required from CRAN, such as 'maps' and 'mapproj'.

 Low-resolution world map

library(ggplot2)

worlddata <- map_data("world") # possible to choose regions see ?world
worldmap <- ggplot(worlddata, aes(x=long, y=lat, group=group)) +
  geom_polygon() +
  scale_y_continuous(breaks=(-2:2) * 30) +
  scale_x_continuous(breaks=(-4:4) * 45)
# Orthographic projection with default orientation (looking down at North pole)
worldmap + coord_map("ortho")
# Centered on North Sea region
worldmap + coord_map("ortho", orientation=c(55, 10, 0))

xxlim <- c(-7,12)   ## selected range North Sea
yylim <- c(48,62)
worldmap +
  coord_cartesian(xlim = xxlim, ylim = yylim)

worldmap +
  coord_cartesian(xlim = xxlim, ylim = yylim) +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.line = element_blank(),
        axis.ticks = element_blank())

Higher resolution maps using package rworldmap and rworldxtra

More info at http://journal.r-project.org/archive/2011-1/RJournal_2011-1_South.pdf

library(rworldmap)
library(ggplot2)
data(countriesLow)

# library(rworldxtra) # for a higher resolution map in combination with next line
# data(countriesHigh)

world <- fortify(countriesLow)
# world <- fortify(countriesHigh)  # for higher resolution map

## this converts any spatialobjectdataframe to a dataframe to be plotted in ggplot

map <- ggplot() +
  geom_polygon(data = world,
               aes(x=long, y=lat, group=group),
               color = "lightgrey", fill = "darkgrey")

xxlim <- c(-7,12)   ## selected range North Sea
yylim <- c(48,62)

map +
  coord_cartesian(xlim = xxlim, ylim = yylim)

map +
  coord_map("ortho", xlim = xxlim, ylim = yylim, orientation=c(55, 10, 0))

map +
  coord_map("stereographic", xlim = xxlim, ylim = yylim, orientation=c(55, 10, 0)) +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.line = element_blank(),
        axis.ticks = element_blank())
  • No labels