【问题标题】:Add a scale bar and a north arrow outside of the plot area of a facetted map plot在多面地图绘图的绘图区域之外添加比例尺和指北针
【发布时间】:2017-08-13 19:34:39
【问题描述】:

我希望在多面地图的绘图区域之外添加一个比例尺和一个指北针。 例如,请考虑以下多面地图图,如 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

我希望在绘图区域之外添加一个比例尺和一个指北针,例如在绘图图例的右侧,以获得如下内容:

我尝试使用 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) 

有人有什么建议吗?提前致谢。

【问题讨论】:

  • 尝试turning off clipping,然后运行代码添加比例尺。
  • @eipi10 我尝试关闭剪辑,但没有奏效。比例尺*消失了(* ggsn 包的 scalebar 函数在出发和到达方面添加了比例尺。删除其中一个比例尺是一个我也不知道如何解决的问题)。

标签: r ggplot2 gis r-grid gtable


【解决方案1】:

我真的想通了!我知道我迟到了 7 个月...

facet.var 中删除'airport$',它突然起作用了。

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) 

【讨论】:

  • 谢谢,我希望他们在文档中展示了如何使用 facet.var 和 facet.lev 的示例。这解决了我的问题。
猜你喜欢
  • 2018-10-24
  • 2015-11-10
  • 2020-08-31
  • 2016-11-28
  • 2016-04-26
相关资源
最近更新 更多