4

I have spatial points which are along a coastline, between years. Using the answer to this, I've plotted the points out and used a slider controlling opacity to show different years.

Is it currently possible to overlay a simple map when using ggvis, potentially using ggmap or any other mapping package?

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)
Community
  • 1
  • 1
user3389288
  • 992
  • 9
  • 30

1 Answers1

2

A little late, but mapview could be a solution for you here (it was only released to CRAN a few days ago).

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)

This does not provide a time-slider or thelike, but will provide popups of the attributes at each points, so the year can be retrieved that way. For a solution with a time-slider you could build a shiny app using mapview.

Cheers Tim

TimSalabim
  • 5,604
  • 1
  • 25
  • 36