Dynamic plotting using ggmap package in R

自古美人都是妖i 提交于 2019-12-11 06:58:50

问题


I am trying to plot my points over the google maps. However, the problem I am facing is I have to set the location points and zoom for every input data file. I tried to use it via variables but its not working. Can anyone help?

Here what I tried:

library(ggmap)
library(ggplot2)
lionmap <- get_map(location=c(70.76, 21.038), zoom=12, maptype="hybrid")

This code only works for static points. How can I make it dynamic?

Here's the head structure for my dataset:

  lion.id       date     time activity      lat     long  distance
1       2 05-05-2002 10:45:00  Feeding 21.14169 70.79053   0.00000
2       2 05-06-2002 10:00:00  Resting 21.14158 70.79314 271.50019
3       2 05-06-2002 19:22:00  Walking 21.14158 70.79314   0.00000
4       2 05-06-2002 19:25:00  Walking 21.14164 70.79267  49.43485
5       2 05-06-2002 19:30:00  Resting 21.14181 70.79222  49.71674
6       2 05-06-2002 23:15:00  Walking 21.14181 70.79222   0.00000

structure(list(lion.id = c(2L, 2L, 2L, 2L, 2L, 2L), date = c("05-05-2002", 
"05-06-2002", "05-06-2002", "05-06-2002", "05-06-2002", "05-06-2002"
), time = c("10:45:00", "10:00:00", "19:22:00", "19:25:00", "19:30:00", 
"23:15:00"), activity = c("Feeding", "Resting", "Walking", "Walking", 
"Resting", "Walking"), lat = c(21.1416944444444, 21.1415833333333, 
21.1415833333333, 21.1416388888889, 21.1418055555556, 21.1418055555556
), long = c(70.7905277777778, 70.7931388888889, 70.7931388888889, 
70.7926666666667, 70.7922222222222, 70.7922222222222), distance = c(0, 
271.500188739303, 0, 49.4348465237462, 49.7167383971771, 0)), .Names = c("lion.id", 
"date", "time", "activity", "lat", "long", "distance"), row.names = c(NA, 
-6L), class = "data.frame")

回答1:


I am not sure what you are after, but I would like to provide two options for you. If you want to create a static map, you can use ggmap.

library(ggplot2)
library(ggmap)

lionmap <- get_map(location = c(70.79, 21.14), zoom = 15, maptype = "hybrid")

ggmap(lionmap) + geom_point(data = mydf, aes(x = long, y = lat), size = 3, color = "white")

If you really want an interactive map, you can use leaflet, for example.

library(leaflet)
library(magrittr)

leaflet(mydf) %>%
addTiles() %>%
setView(lng = 70.79, lat = 21.14, zoom = 15) %>%
addCircleMarkers()

DATA

mydf <- structure(list(lion.id = c(2L, 2L, 2L, 2L, 2L, 2L), date = c("05-05-2002", 
"05-06-2002", "05-06-2002", "05-06-2002", "05-06-2002", "05-06-2002"
), time = c("10:45:00", "10:00:00", "19:22:00", "19:25:00", "19:30:00", 
"23:15:00"), activity = c("Feeding", "Resting", "Walking", "Walking", 
"Resting", "Walking"), lat = c(21.1416944444444, 21.1415833333333, 
21.1415833333333, 21.1416388888889, 21.1418055555556, 21.1418055555556
), long = c(70.7905277777778, 70.7931388888889, 70.7931388888889, 
70.7926666666667, 70.7922222222222, 70.7922222222222), distance = c(0, 
271.500188739303, 0, 49.4348465237462, 49.7167383971771, 0)), .Names = c("lion.id", 
"date", "time", "activity", "lat", "long", "distance"), row.names = c(NA, 
-6L), class = "data.frame")


来源:https://stackoverflow.com/questions/32164452/dynamic-plotting-using-ggmap-package-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!