[英]Dynamic plotting using ggmap package in R
我正在嘗試在Google地圖上標出我的觀點。 但是,我面臨的問題是我必須為每個輸入數據文件設置位置點並進行縮放。 我試圖通過變量使用它,但是它不起作用。 有人可以幫忙嗎?
這是我嘗試的:
library(ggmap)
library(ggplot2)
lionmap <- get_map(location=c(70.76, 21.038), zoom=12, maptype="hybrid")
此代碼僅適用於靜態點。 如何使其具有動態性?
這是我的數據集的頭部結構:
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")
我不確定您要做什么,但是我想為您提供兩個選擇。 如果要創建靜態映射,可以使用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")
如果您確實需要交互式地圖,則可以使用傳單。
library(leaflet)
library(magrittr)
leaflet(mydf) %>%
addTiles() %>%
setView(lng = 70.79, lat = 21.14, zoom = 15) %>%
addCircleMarkers()
數據
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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.