r语言 - 绘制NASA "black marble"地理上的Airbnb数据



我正试图创建一个类似于Airbnb的Ricardo Bion所展示的图像,但我想在NASA的"黑色大理石"图像上绘制可视化效果,以提供更多的上下文,因为我没有Airbnb数据集的数据密度。

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

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

以下是我尝试使用ggplot2 annotation_graster选项在绘图后面分层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") 

请注意,应先绘制背景,然后再绘制线条和点,否则图像将覆盖其他绘图元素。

最新更新