在 NASA 的 "black marble" geotiff 上绘制 Airbnb 数据

Plotting Airbnb data over NASA's "black marble" geotiff

我正在尝试创建一个与 Ricardo Bion of Airbnb 提供的图像类似的图像,但我想在 NASA "black marble" 图像上绘制可视化效果以提供更多上下文,因为我几乎没有Airbnb 数据集的数据密度。

我使用全球地图 13500x6750(3 公里)GeoTIFF 39 MB 选项下载了 Nasa 黑色大理石图像 here

我一直 运行 关注的这个问题是,在过去几年中,在线提供的大多数选项和解释都已贬值。我尝试使用 EBImage,如图所示 here but ebimageGrob has been removed from gridExtra. I also tried to use the rasterVis package as shown 但代码在可着色步骤中断。

据我所知,这是我尝试使用 ggplot2 annotation_raster 选项将 tiff 分层(这给出了目的地之间的线条,但只有白色背景):

library(ggplot2)
library(ggmap)
library(sp)
library(grid)
library(geosphere)
library(plyr)
library(tiff)

# source the theme_map for ggplot2
# source("https://dl.dropboxusercontent.com/u/2364714/theme_map.R")

# in the original post I had a data.frame with 500k rows of top origin destination pairs
trips <- data.frame(origin = c("San Francisco", "Sydney", "Chicago"), 
                destination = c("Paris", "Tokyo", "Boston"), 
                stringsAsFactors = FALSE)


# get lat and lon of cities
trips$geocode_origin <- suppressMessages(geocode(trips$origin))
trips$geocode_destination <- suppressMessages(geocode(trips$destination))


# get intermediate points between the two locations
arch <- gcIntermediate(trips$geocode_origin,
                   trips$geocode_destination,
                   n=100,
                   breakAtDateLine=FALSE, 
                   addStartEnd=TRUE, sp=TRUE)

# http://docs.ggplot2.org/0.9.3.1/fortify.map.html
arch_fortified <- ldply(arch@lines, fortify)

earth <- readTIFF("~/Downloads/dnb_land_ocean_ice.2012.13500x6750_geo.tif")

theme_map <- function(base_size = 12) {
  require(grid)
  theme_grey(base_size) %+replace%
    theme(
      axis.title = element_blank(),
      axis.text = element_blank(),
      panel.grid = element_blank(),
      axis.ticks.length = unit(0,"cm"),
      panel.margin = unit(0,"lines"),
      plot.margin = unit(c(0,0,0,0),"lines"),
      complete = TRUE, 
      panel.background = element_rect(fill = NA, colour=NA)
    )
}

# a few lines of ggplot2 code
ggplot() +
  geom_line(aes(long,lat,group=group), data=arch_fortified, alpha=0.1,size=1, colour="skyblue1") +
  coord_cartesian(ylim =c(-45, 70), xlim=c(-165, 165)) +
  theme_map() +
  geom_point(aes(lon, lat),data=trips$geocode_origin, alpha = 0.8, size = 1, colour = "white") +
  geom_point(aes(lon, lat),data=trips$geocode_destination, alpha = 0.8, size = 1, colour = "white") + 
  annotation_raster(earth, -180, 180, -90, 90)

谢谢!

我只需要稍微修改一下你的绘图代码就可以让它工作:

ggplot(arch_fortified) +
  coord_cartesian(ylim =c(-45, 70), xlim=c(-165, 165)) +
  theme_map() +
  annotation_raster(earth, -180, 180, -90, 90) +
  geom_line(aes(long,lat,group=group), alpha=0.1,size=1, colour="skyblue1") +
  geom_point(aes(lon, lat),data=trips$geocode_origin, alpha = 0.8, size = 1, colour = "white") +
  geom_point(aes(lon, lat),data=trips$geocode_destination, alpha = 0.8, size = 1, colour = "white") 

注意先画背景,再画线和点,否则图像会覆盖其他情节元素。