You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

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'.
More info at http://journal.r-project.org/archive/2011-1/RJournal_2011-1_South.pdf

 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

library(rworldmap)
library(ggplot2)
data(countriesLow)
world <- fortify(countriesLow)
## 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