### Visualizing data # Loading the caribou data frame and looking at locations per caribou load("./_data/caribou.daily.df.rda") head(caribou.daily.df) table(caribou.daily.df$id) # Subsetting to one caribou and making some plots of it's movement FY1607 <- subset(caribou.daily.df, id == "FY1607") with(FY1607, plot(x,y,asp=1, type="o")) par(mar = c(0,4,0,0), oma = c(4,0,5,0), xpd=NA) layout(rbind(c(1,2), c(1,3))) with(FY1607, { plot(x, y, asp = 1, type="o") plot(time, x, type="o", xaxt="n", xlab="") plot(time, y, type="o") title(FY1607$id[1], outer=TRUE) }) # Creating the scan.track funciton scan.track <- function(x,y,time, ...){ par(mar = c(0,4,0,0), oma = c(4,0,5,4), xpd=NA) layout(rbind(c(1,2), c(1,3))) plot(x, y, asp = 1, type="o", ...) plot(time, x, type="o", xaxt="n", xlab="", ...) plot(time, y, type="o", ...) } # Trying it with another caribou with(subset(caribou.daily.df, id == "FA1403"), scan.track(x,y,time,pch=19, col=rgb(1,0,0,.5), cex=0.5)) # Making some PDFs (don't need to run this in the workshop, but the code is here in case you need it in the future!) # pdf("caribouscans.pdf", width = 6, height = 3) # for(myname in levels(caribou.daily.df$id)){ # me <- subset(caribou.daily.df, id == myname) # scan.track(me$x, me$y, me$time, pch=19, col=rgb(0,0,0,.5), cex=0.5) # title(myname, outer=TRUE) # } # dev.off() # more PDF creation # pdf("myanimation.pdf") # for(i in 1:nrow(FY1607)){ # with(FY1607, { # plot(x,y, asp=1, type="l", col="grey") # lines(x[1:i], y[1:i], col="blue", cex=0.5, lwd=2) # points(x[i], y[i], pch=21, cex=2, col="lightblue") # title(paste(id[1], day.date[i])) # # }) # box() # } # dev.off() # # A quick ggplot require(ggplot2) ggplot(caribou.daily.df, aes(x=x, y=y, col=id)) + geom_path(alpha = 0.5) + geom_point(alpha = 0.5) + coord_fixed() # now incorporting a Google basemap require(ggmap) latrange <- range(caribou.daily.df$lat) + c(-.5,.5) longrange <- range(caribou.daily.df$lon) + c(-.5,.5) caribou.map <- get_map(location = c(longrange[1], latrange[1], longrange[2], latrange[2]), maptype = "terrain", source="google") # You could also get this using the code below, modifying the zoom argument as necessary ## caribou.map <- get_map(bbox(caribou.move), source="google", zoom=8, maptype="terrain") # plotting the empty basemap ggmap(caribou.map) # And the version with the caribou data overlay ggmap(caribou.map) + geom_point(data = caribou.daily.df, mapping = aes(x = lon, y = lat, col=id), alpha = 0.6, size=1) + geom_path(data = caribou.daily.df, mapping = aes(x = lon, y = lat, col=id), alpha = 0.6, size=1) + coord_map() + labs(x = "Longitude", y = "Latitude") + scale_color_discrete(l = 65) + guides(color=guide_legend(ncol=2)) # And now for an interactive map! require(mapview) require(sp) caribou.sp <- caribou.daily.df coordinates(caribou.sp)<- ~lon + lat proj4string(caribou.sp)<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") mapview(caribou.sp, zcol="id", legend = TRUE, cex=5, lwd=2, map.type = "Esri.DeLorme") # This is code for creating individual Google Earth kml files. # Again, don't need to run this during the workshop, unless you want to! # require(maptools) # require(gplots) # palette(rich.colors(4)) # for(myid in unique(caribou.daily.df$id)){ # m <- subset(caribou.daily.df, id == myid)[c("lon", "lat")] # m.lines <- Lines(Line(m), ID = myid) # kmlLine(m.lines, kmlfile = paste0(myid,".kml"), # kmlname = myid, description = myid, # lwd=4, # col=match(myid, unique(caribou.daily.df$id))) # } load("./_data/caribou.df.rda") # Plotting a caribou with the track colored by elevation ggplot(subset(caribou.df, id == "FY1607"), aes(x = lon, y = lat, color=elevation)) + geom_path() + geom_point() + scale_color_continuous(na.value="transparent")#, limits = c(1400, 2500)) # Same thing but with multiple caribou: ggmap(caribou.map, darken = c(0.5, "white")) + geom_point(data = caribou.df, aes(x = lon, y = lat, color = elevation), size =2) + scale_color_gradientn(colors=terrain.colors(10), na.value="transparent") + geom_point(shape=1, size = 2, color = "black") + coord_map() + labs(x = "Longitude", y = "Latitude")