thackl / ggworldmap

Visualize Global-Scale Quantitative Data on World Maps with Projection and Shifted Central Meridian
MIT License
7 stars 3 forks source link

[[img/header.png]]

<Description & Motivation>

** Inspiration

** Installation

+BEGIN_SRC R

library(devtools) devtools::install_github("thackl/ggworldmap")

+END_SRC

** Usage

+BEGIN_SRC R

library(tidyverse) library(ggworldmap)

a nice projected map with graticules and degree labels

ggworldmap(proj = "moll", degree = list("light", long_by = 80))

and short for

proj <- "moll" ggplot() + geom_worldmap(proj = proj, color = "gray90", fill = "gray90") + geom_graticule(proj = proj, color = "gray80") + geom_gratframe(proj = proj, color = "gray80") + geom_degree(proj = proj, color = "gray80", long_by = 80) + theme_worldmap_light() + coord_equal()

ggsave("img/ggworldplot.png", width = 12, height = 6, type = "cairo")

+END_SRC

[[img/ggworldplot.png]]

+BEGIN_SRC R

Pacific centered map with colored countries

long_0 <- 110 ggworldmap(proj = "ortho", long_0 = long_0, mapping = aes(fill=group), long_min = long_0-90, long_max = long_0+90, color = "black", show.legend = FALSE, graticule = list("light", color = "grey20"), degree = list("light", long_n = 3, color = "grey30", family = "Times New Roman")) + scale_fill_distiller(palette = "Pastel2")

ggsave("img/east-asia-countries.png", width = 12, height = 6, type = "cairo")

+END_SRC

[[img/east-asia-countries.png]]

+BEGIN_SRC R

The Pacific Ring of Fire

data(volcanic_eruptions) # load example data proj <- "robin" # Robinson projection long_0 <- -150 # Center on Pacific ve_proj <- volcanic_eruptions %>% project(proj, long_0) %>% # project data arrange(desc(VEI)) # and get nice plotting order

plot projected map and data

ggworldmap(ve_proj, long_0 = long_0, proj = proj) + geom_point(aes(size = VEI^4, color = VEI), alpha =.5) + scale_color_distiller(palette = "Spectral")

ggsave("img/pacific-ring-of-fire.png", width = 12, height = 6, type = "cairo")

+END_SRC

[[img/pacific-ring-of-fire.png]]

+BEGIN_SRC R

TODO: Project and Concentrate Data

+END_SRC

+BEGIN_SRC R

proj <- "robin" # use Robinson projection long_0 <- -120 # center on Pacific lat_min <- -70 # and clip the poles lat_max <- 70

get some sample data

data(volcanic_eruptions) volcanic_eruptions <- volcanic_eruptions %>% filter(Year > 1919) %>% # let's look at last 100 years project(proj, long_0) # and we want our data projected volcanic_eruptions_decluttered <- volcanic_eruptions %>% concentrate(lambda = .2) %>% # round up close points at a single location arrange(Type) # and order them nicely for plotting

set up a plot of the world

gg <- ggplot(mapping = aes(x=long, y=lat)) +

----------------------------------------------------------------------------

get a world map, centered on the Pacific, in Robinson projection

geom_worldmap(proj = proj, long_0 = long_0, lat_min = lat_min, lat_max = lat_max, color = "grey90", fill = "grey90") +

add some grid lines

geom_graticule(proj = proj, long_0 = long_0, lat_min = lat_min, lat_max = lat_max, long_by = 40, linetype = 1, color = "grey80", size = .5, alpha=.3) +

and a grid frame

geom_graticule(proj = proj, long_0 = long_0, lat_min = lat_min, lat_max = lat_max, lat_n = 2, long_n = 2, linetype = 1, color = "grey80", size = 1) +

and some grid labels

geom_degree(proj = proj, long_0 = long_0, lat_min = lat_min, lat_max = lat_max, long_by = 40, color = "grey60", size = 3)

----------------------------------------------------------------------------

now throw our data on the canvas

geom_array(aes(color = Type), volcanic_eruptions_decluttered, nrow = 7, spread = 2, shape = 19, size = .8, vjust = .5) +

original locations with overplotting issues

geom_point(data = volcanic_eruptions, size = .3, alpha = .5) +

----------------------------------------------------------------------------

and finally make things pretty

ggtitle("Significant Volcanic Eruptions", "of the last 100 years by most common types") +

with fixed aspect ratio, but no poles,

coord_equal(ylim = c(-80, 80)) +

an almost void greyish theme,

theme_light(base_size = 15) + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), legend.justification=c(0,1), legend.position=c(.52,.29), legend.text.align=0) +

and nice colors

scale_color_brewer("Eruption types", palette="Dark2") +

wrapped in facets

facet_wrap(~Type, ncol=2, strip.position = "left")

+END_SRC

[[img/volcanic-eruptions.png]]