2017-03-21 9 views
3

ファセットマッププロットのプロット領域外にスケールバーと北矢印を追加したいとします。例として 、マックス・マルキ(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 私はプロットエリアの外のスケールバーと北の矢印を追加します、例えばプロット伝説の右側に、このような何かを取得する:

enter image description here

私はggsnパッケージのscalebarnorth機能を使用してみました、しかし、これらの関数は、プロット領域の内側にスケールバーとノース矢印を追加することしかできません。これらの機能を使用する私の試みは次のとおりです:

# 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) 

誰にも示唆はありますか?前もって感謝します。

+0

(http://stackoverflow.com/a/9691256/496488)[クリッピングをオフにする]試してみて、その後、スケールバーを追加するためのコードを実行します。 – eipi10

+0

@ eipi10クリッピングをオフにしてみましたが、うまくいきませんでした。スケールバー*が消えました( 'ggsn'パッケージの' scalebar'関数は、出発点と到着点に尺度バーを追加しました)。これらの尺度バーの1つを削除することは、解決方法もわかりません)。 –

答えて

1

私は実際にそれを理解しました!私はショーに7ヶ月遅れているのを知っています...

「空港$」をfacet.varから削除して突然動いています。

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

関連する問題