6

I wish to add a scale bar and a north arrow outside of the plot area of a facetted map plot. As an example, consider the following facetted map plot, as seen in a blog post by Max Marchi (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 <- factor("departures")
airportA$type <- factor("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 map plot 
library(ggplot2)
facet.gmap <- ggmap(map) +
  geom_point(aes(x = lon, y = lat, 
  size = sqrt(flights)), 
  data = airportDA, alpha = .5) +
  scale_size(breaks = sqrt(c(1, 5, 10, 50, 100, 500)), 
  labels = c(1, 5, 10, 50, 100, 500), name = "routes") + 
  theme(legend.position = "bottom",
        legend.direction="horizontal") +
  guides(size=guide_legend(nrow=1)) +  
  facet_wrap( ~ type, ncol=2)  
facet.gmap

enter image description here I wish to add a scale bar and a north arrow outside of the plot area, at the right side of the plot legend for instance, to obtain something like this:

enter image description here

I tried using the scalebar and north functions of the ggsn package, however these functions only allow adding scale bars and north arrows inside of the plot area. My attempts using these functions are as follows:

# Adding a scale bar (but can't positioned it outside of the plot area)
library(ggsn)
bb <- attr(map, "bb")
bb2 <- data.frame(long = unlist(bb[c(2, 4)]), lat = unlist(bb[c(1,3)]))

scale.bar <- facet.gmap +
  scalebar(data = bb2, dist = 250, dd2km = TRUE, 
    model  = "WGS84",
    st.size=2.5,
    height=0.015,  
    location = "topleft",
    facet.var='airportDA$type',
    facet.lev='departures', 
    anchor = c(
      x = bb$ll.lon + 0.05 * (bb$ur.lon - bb$ll.lon), 
      y = bb$ll.lat + 0.9 * (bb$ur.lat - bb$ll.lat) )) 
scale.bar

# Adding a north arrow (but can't positioned it outside of the plot area)
north2(scale.bar,x=0.12,y=0.90, scale=0.09, symbol=3) 

Does anyone have any suggestion? Thanks in advance.

R. Joe
  • 367
  • 5
  • 13
  • Try [turning off clipping](http://stackoverflow.com/a/9691256/496488) and then run the code to add the scale bar. – eipi10 Mar 21 '17 at 22:19
  • @eipi10 I tried turning off clipping but it didn't work. The scale bar* disappeared (* the `scalebar` function of the `ggsn` package added a scale bar to the departures and to the arrivals facet. Removing one of these scale bars is an issue that I also don’t know how to solve). – R. Joe Mar 22 '17 at 07:13

1 Answers1

5

I actually figured it out! I know I'm like 7 months late to the show...

remove 'airport$' from facet.var, and it suddenly works.

scale.bar <- facet.gmap +
  scalebar(data = bb2, dist = 250, dd2km = TRUE, 
           model  = "WGS84",
           st.size=2.5,
           height=0.015,  
           location = "topleft",
           facet.var='type',
           facet.lev="arrivals", 
           anchor = c(
             x =-10, 
             y = 65 )) 
scale.bar

# Adding a north arrow (but can't positioned it outside of the plot area)
north2(scale.bar,x=0.82,y=0.17, scale=0.09, symbol=3) 

enter image description here

Robin Choudhury
  • 304
  • 4
  • 10
  • 1
    Thanks, I wish they showed an example of how to use facet.var and facet.lev in the documentation. This solved my problem. – Nova Aug 21 '18 at 13:40