使用R中的ggmap包进行动态绘图

时间:2015-08-23 07:57:33

标签: r ggmap

我试图在谷歌地图上绘制我的观点。但是,我面临的问题是我必须为每个输入数据文件设置位置点和缩放。我试图通过变量使用它,但它无法正常工作。有人可以帮忙吗?

这是我试过的:

    var figure = $(".video");
    var vid = figure.find("video");

    [].forEach.call(figure, function (item,index) {
        item.addEventListener('mouseover', hoverVideo.bind(item,index), false);
        item.addEventListener('mouseout', hideVideo.bind(item,index), false);
    });

    function hoverVideo(index, e) {
        vid[index].play(); 
    }

    function hideVideo(index, e) {
        vid[index].pause(); 
    }

此代码仅适用于静态点。我怎样才能让它变得动态?

这是我的数据集的头部结构:

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

1 个答案:

答案 0 :(得分:3)

我不确定你的目标是什么,但我想为你提供两种选择。如果要创建静态地图,可以使用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")

enter image description here

如果你真的想要一张交互式地图,你可以使用传单,例如。

library(leaflet)
library(magrittr)

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

enter image description here

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")