在美国国家航空航天局(NASA)的黑色大理石上绘制Airbnb数据"的GeoTIFF

时间:2016-05-01 18:31:51

标签: r gis

我正在尝试创建一个类似于Ricardo Bion of Airbnb所呈现的图像,但我想将可视化绘制在NASA"黑色大理石"图片提供更多背景信息,因为我几乎没有Airbnb数据集的数据密度。

我使用全球地图13500x6750(3公里)GeoTIFF 39 MB选项下载了美国国家航空航天局黑色大理石图像here

我一直遇到的这个问题是大多数选项和在线提供的解释在过去几年中已被折旧。我尝试使用EBImage,如here所示,但ebimageGrob已从gridExtra中删除。我还尝试使用rasterVis包,如图所示here,但代码在可着色步骤中断开。

据我所知,尝试使用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)

谢谢!

1 个答案:

答案 0 :(得分:1)

我只需稍微修改您的绘图代码即可使其正常工作:

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

请注意,您应首先绘制背景,然后才能绘制直线和点,否则图像将覆盖其他绘图元素。