Closed mdsumner closed 5 years ago
library(dplyr) library(raadtools) library(SOmap) ele <- SOmap_data$mirounga_leonina polar_proj <- "+proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0" ele[c("x", "y")] <- rgdal::project(as.matrix(ele[c("lon", "lat")]), polar_proj) ## raadtools normalizes all dates to the temporal sequence of the env data ice_raster <- readice(ele$date) length(getZ(ice_raster)) range(getZ(ice_raster)) range(diff(getZ(ice_raster))) roi <- round(c(range(ele$lon), range(ele$lat)) + c(-2, 2, -2, 2)) bx <- projectRaster(readtopo("etopo2", xylim = roi), raster(extent(range(ele$x) + c(-1, 1) * 5e5, range(ele$y) + c(-1, 1) * 5e5), nrows = 300, ncols = 300, crs = polar_proj)) # 15 % is a standard "ice edge" threshold contour_raster_sf <- function(x, level = 15, date = as.POSIXct(NA, tz = "UTC")) { raster::rasterToContour(x, level = level, maxpixels = prod(dim(x[[1]]))) %>% sf::st_as_sf() %>% dplyr::mutate(date = date) } ice_edge <- vector("list", nlayers(ice_raster)) for (i in seq_along(ice_edge)) { ice_edge[[i]] <- contour_raster_sf(ice_raster[[i]], date = getZ(ice_raster)[i]) } ice_edge <- sf::st_cast(do.call(rbind, ice_edge), "MULTILINESTRING") bathy_data <- as.data.frame(bx, xy = TRUE) %>% dplyr::filter(!is.na(z)) bathy_data$date <- NULL library(gganimate) g <- ggplot() + # geom_raster(data = bathy_data, aes(x, y, fill = z)) + geom_sf(data = ice_edge, colour = "black") + geom_point(data = ele, aes(x, y, colour = id)) + theme_bw() + xlim(range(ele$x) + c(-1, 1) * 2e5) + ylim(range(ele$y) + c(-1, 1) * 2e5) + transition_time(date) ##I"m finding this fails with geom_raster or geom_path used above g ## suggested to fix problem when more time steps than frames # gganimate::animate(g, detail = 3)
Cooooooooooool! Tweaked a bit and added, https://github.com/AustralianAntarcticDivision/ropenblog/commit/3f7f2eaeddeb78f8c1905808b055b8f3e8d52b3a.