I wish to make a facetted plot of Google maps with an overall label and the label having different font sizes. For instance, consider the following codes, which are based on the codes provided by Max Marchi in a blog post (link):
# Load the data
airports <- read.csv("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat", header = FALSE)
colnames(airports) <- c("ID", "name", "city",
"country", "IATA_FAA", "ICAO", "lat", "lon",
"altitude", "timezone", "DST")
routes <- read.csv("https://github.com/jpatokal/openflights/raw/master/data/routes.dat", header = FALSE)
colnames(routes) <- c("airline", "airlineID",
"sourceAirport", "sourceAirportID",
"destinationAirport", "destinationAirportID",
"codeshare", "stops", "equipment")
# Getting the data ready for plotting
# * For a detailed explanation on setting up the data
# I suggest consulting Max Marchi's post:
# http://www.milanor.net/blog/maps-in-r-plotting-data-points-on-a-map/
library(plyr)
departures <- ddply(routes, .(sourceAirportID), "nrow")
names(departures)[2] <- "flights"
arrivals <- ddply(routes, .(destinationAirportID), "nrow")
names(arrivals)[2] <- "flights"
airportD <- merge(airports, departures, by.x = "ID",
by.y = "sourceAirportID")
airportA <- merge(airports, arrivals, by.x = "ID",
by.y = "destinationAirportID")
airportD$type <- "departures"
airportA$type <- "arrivals"
# The final data frame used for plotting
airportDA <- rbind(airportD, airportA)
# Get the map of Europe from Google Maps
library(ggmap)
map <- get_map(location = 'Europe', zoom = 4)
# Make a facetted Google map plot
library(ggplot2)
facet.gmap <- ggmap(map) +
geom_point(aes(x = lon, y = lat,
size = sqrt(flights)),
data = airportDA, alpha = .5) +
facet_wrap( ~ type, ncol=2) +
theme(legend.position="none")
# Add an overall label with different font sizes
library(gtable)
library(grid)
facet.gmap.label <- ggplotGrob(facet.gmap)
facet.gmap.label <- gtable_add_grob(facet.gmap.label,
grobTree(textGrob("M", x=0.05,
y=0.85,just="left",
gp = gpar(fontsize = 14,
fontface = "bold")),
textGrob("Some label", x=0.18,
y=0.68, just="left",
gp = gpar(fontsize = 9,
fontface = "bold")) ),
t=1, b=4, l=1, r=4)
# Save as PDF
pdf("facet.gmap.label.pdf",
width=4.5,
height=3.6)
grid.draw(facet.gmap.label)
dev.off()
I get this:
To reduce the white blank spaces and make the overall label visible I tested different values in the theme
's plot.margin
parameter, and the “best” (least bad) plot was obtained by setting plot.margin = unit(c(0.8, 0.4, -3.8, 0.3), "lines")
:
# Edit the 'theme' 'plot.margin' parameter
facet.gmap2 <- facet.gmap +
theme(plot.margin = unit(c(0.8, 0.4, -3.8, 0.3), "lines"))
# Add again the overall label with different font sizes
facet.gmap.label2 <- ggplotGrob(facet.gmap2)
facet.gmap.label2 <- gtable_add_grob(facet.gmap.label2,
grobTree(textGrob("M", x=0.05,
y=0.85,just="left",
gp = gpar(fontsize = 14,
fontface = "bold")),
textGrob("Some label", x=0.18,
y=0.68, just="left",
gp = gpar(fontsize = 9,
fontface = "bold")) ),
t=1, b=4, l=1, r=4)
# Save as PDF
pdf("facet.gmap.label2.pdf",
width=4.5,
height=3.6)
grid.draw(facet.gmap.label2)
dev.off()
Result:
Bottom line: as much as I test different values in plot.margin
, I still don’t get the plot that I need, which would be something like this:
I made this last/desired plot with the help of an image editor software however for my goals this is not a good idea as I need to make several of these facetted Google map plots; each with an overall label and the label having different font sizes.
Does anyone have any suggestion of how to make a plot like this last one in R? Thanks in advance.
P.S.1: The black borders around the figures above were drawn manually with an image editor software to highlight the issue that I’m having with white blank spaces.
P.S.2: In the codes provided I exported the plots as PDF because the graphics are for publishing use; and hence too much white blank spaces is not a good thing since scientific journals often have limits for figure dimensions.
Why can't you just use ggtitle
, which will automatically handles margin additions?
This should give you the text output you want without having to play around with finding the exact x-y coordinates you need:
facet.gmap +
ggtitle(expression(bold(M)~scriptstyle("Some Label") ))
gives the plot you are looking for.
The other issue you seem to be having is the aspect ratio. ggmap
(helpfully) insists on using coord_fixed
or similar to ensure that the ratio of x to y distances are correctly preserved. If the aspect ratio of your output (the width
to height
in your call to pdf
) is different, the plot will be scaled down to fit the dimension that is most restrictive and leave white space elsewhere. I fiddled with it in the plot window in RStudio and got a pretty good ratio at 440x287 (what I used to export). You might try starting with a width to height that preserves that approximate ratio and tinkering from there.