Skip to content

Instantly share code, notes, and snippets.

@mattbaggott
Created December 16, 2012 19:25
Show Gist options
  • Save mattbaggott/4311763 to your computer and use it in GitHub Desktop.
Save mattbaggott/4311763 to your computer and use it in GitHub Desktop.
ggsubplot allows embedding of one ggplot in another. Variants of this are commonly requested, although often separate plots are better. Package is quite slow. ggsubplot uses but does not include code for map_afghanistan and map_americas layers, which are included here.
##
## Vignette code for ggsubplot with needed map_afghanistan and map_americas layers
## matt@baggott.net
## Dec 16, 2012
# install.packages("ggsubplot")
library(ggplot2)
library(ggsubplot)
library(maps)
library(plyr)
# getbox by Heike Hoffman,
# https://github.com/ggobi/paper-climate/blob/master/code/maps.r
getbox <- function (map, xlim, ylim) {
# identify all regions involved
small <- subset(map, (long > xlim[1]) & (long < xlim[2]) & (lat > ylim[1]) & (lat < ylim[2]))
regions <- unique(small$region)
small <- subset(map, region %in% regions)
# now shrink all nodes back to the bounding box
small$long <- pmax(small$long, xlim[1])
small$long <- pmin(small$long, xlim[2])
small$lat <- pmax(small$lat, ylim[1])
small$lat <- pmin(small$lat, ylim[2])
# Remove slivvers
small <- ddply(small, "group", function(df) {
if (diff(range(df$long)) < 1e-6) return(NULL)
if (diff(range(df$lat)) < 1e-6) return(NULL)
df
})
small
}
## map layer
## adapted from map_nasa:
# https://github.com/ggobi/paper-climate/blob/master/code/maps.r
# assembling data
world <- map_data("world")
states <- map_data("state")
states$group <- max(world$group) + states$group
both <- rbind(world, states)
americas <- getbox(both, xlim = c(-115, -55), ylim = c(-21.1, 36.6))
# building americas layer
map_americas <- list(
geom_polygon(aes(long, lat, group = group), data = americas, fill = "grey70",
colour = "grey60", inherit.aes = FALSE, show_guide = FALSE),
scale_x_continuous("", breaks = NULL, expand = c(0.02, 0)),
scale_y_continuous("", breaks = NULL, expand = c(0.02, 0)))
# building afghanistan layer
afghanistan <- getbox(world, c(60,75), c(28, 39))
map_afghanistan <- list(
geom_polygon(aes(long, lat, group = group), data = afghanistan,
fill = "white", colour = "black", inherit.aes = FALSE,
show_guide = FALSE),
scale_x_continuous("", breaks = NULL, expand = c(0.02, 0)),
scale_y_continuous("", breaks = NULL, expand = c(0.02, 0)))
## Afghan War Journal raw data
ggplot(casualties) +
map_afghanistan +
geom_point(aes(lon, lat, color = victim)) +
coord_map()+
theme(legend.position = "none", legend.key = element_rect(fill = 'white'))
## 2d bin with bar chart subplots displaying data in each region
ggplot(casualties) +
map_afghanistan +
geom_subplot2d(aes(lon, lat,
subplot = geom_bar(aes(victim, ..count.., fill = victim))),
bins = c(15,12), ref = NULL, width = rel(0.8)) +
coord_map()+
theme(legend.position = "right")
## Stars in an embedded plot
ggplot(nasa) +
map_americas +
geom_subplot(aes(long, lat, group = id,
subplot = geom_star(aes(x = 0, y = 0, r = surftemp,
angle = date, fill = mean(surftemp)), r.zero = FALSE))) +
coord_map()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment