如何在地图上使用ggvis和映射包(例如ggmap)交互地绘制空间数据?

4

我有一些沿海岸线的空间点数据,跨越多年。根据这个答案,我已经将这些点绘制出来,并使用控制透明度的滑块显示不同年份。

在使用ggvis时,是否可以叠加一个简单的地图,可能使用ggmap或任何其他地图包?

library(ggmap)
library(ggvis)

#simple dataset
long <- c(-53.53167, -53.53167, -53.68000, -53.64667, -53.68500)
lat <- c(47.16833, 47.18500, 47.01167, 47.00500, 47.98833)
year <- c(1995:1999)
count <- rpois(5, 20)
data1 <- as.data.frame(cbind(year, count, lat, long))

#interactive data visual, with the goal of having a map in the background
data %>% 
  ggvis(~long, ~lat, size=~count) %>% 
  layer_points(opacity=input_slider(min(data$year), max(data$year),     step=1,map=function(x) ifelse(data$year == x, 1, 0)))

#in ggmap, an example of a potential map to make interactive
mylocation <- c(min(long), min(lat))
mymap <- get_map(location=mylocation, source="google", maptype="roadmap", zoom = 8)
ggmap(mymap) + geom_point(data=data1, aes(x=long, y=lat, size=count), alpha=0.6)

2
如果我没记错的话,ggvis目前还不能处理地图。 - jazzurro
1个回答

2

有点晚了,但是mapview可能是您的一个解决方案(它只在几天前发布到CRAN)。

library(ggmap)
library(ggvis)
library(sp)
library(mapview)

#simple dataset
long <- c(-53.53167, -53.53167, -53.68000, -53.64667, -53.68500)
lat <- c(47.16833, 47.18500, 47.01167, 47.00500, 47.98833)
year <- c(1995:1999)
count <- rpois(5, 20)

# convert to SpatialPointsDataFrame
data1 <- as.data.frame(cbind(year, count, lat, long))
proj4string(data1) <- CRS("+init=epsg:4326")

# view it
mapview(data1)

这并不提供时间滑块或类似的功能,但会在每个点提供属性的弹出窗口,因此可以通过这种方式检索年份。如果需要带有时间滑块的解决方案,您可以使用mapview构建一个闪亮的应用程序。

祝好 Tim


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接