4

我正在尝试绘制我拥有的 gps 点路径(包含的示例是荷兰的火车)。我想利用包的 shadow_mark()view_follow()功能,gganimate()使情节开始完全放大,然后逐渐缩小。如果我不使用 google 或 OSM 地图作为背景,这很好用,但我想使用其中之一(不挑剔)。我试图玩弄这个exclude_layer =命令,但似乎无法让它工作。如果我使用地图作为背景,动画将从第一帧缩小。

我环顾四周,但找不到view_follow()与地图一起用作背景的示例。

设置包和数据(200 gps 点)

require(gganimate)
require(tidyverse)
require(lubridate)
require(ggmap)
library(OpenStreetMap)


df3 <- structure(list(lat = c(52.0473442, 52.0173027, 52.0898208, 52.0309643, 
                              52.0497488, 52.0897325, 52.0892640857143, 52.0179681, 52.0383389, 
                              52.1048323, 52.0181769, 52.0289188, 52.0941033, 52.0182591, 52.0882347, 
                              52.090628, 52.1060504, 52.0893178, 52.1062902, 52.015563475, 
                              52.1041445, 52.09096297, 52.0095689, 52.0460849, 52.0949215, 
                              52.0243182, 52.066435, 52.0273662, 52.1055631, 52.0893175, 52.0487954, 
                              52.0980435, 52.09089341, 52.0145649, 52.0089167, 52.0605713, 
                              52.009809, 52.0895006516129, 52.0893323, 52.0959561, 52.0659477, 
                              52.0514133, 52.08956736, 52.0898078, 52.0971748, 52.0131978, 
                              52.049002, 52.0182712, 52.1045885, 52.0895501, 52.062702, 52.0919686, 
                              52.0163620216216, 52.09101514, 52.0895122571429, 52.0906238, 
                              52.1049253, 52.0085885, 52.0948177, 52.0906366, 52.0894994903226, 
                              52.103213, 52.0372776, 52.0468101, 52.0485351, 52.0874545, 52.0392919, 
                              52.0892646285714, 52.08922625, 52.0143929, 52.0503487, 52.0598063, 
                              52.0894236363636, 52.0895004193548, 52.0895104285714, 52.08926629, 
                              52.0204065, 52.0494409, 52.0894992580645, 52.0149126096774, 52.0910172, 
                              52.0163522, 52.1038697666667, 52.0175865, 52.1011104, 52.1022396, 
                              52.0508854, 52.0326581, 52.0893379636364, 52.0936814, 52.0608923, 
                              52.0171837, 52.0207873, 52.0892670714286, 52.089319, 52.1019613, 
                              52.1005872, 52.0430951, 52.0898355, 52.0893309181818, 52.0180603, 
                              52.0390661, 52.0348046, 52.0481463, 52.0420622, 52.0167229, 52.0523997, 
                              52.0172069, 52.0412617, 52.0212251, 52.0894450545455, 52.0381603, 
                              52.0988658, 52.0892341636364, 52.0167707108108, 52.0856953, 52.0455796, 
                              52.0893241, 52.0974118, 52.10305028, 52.0975816, 52.0906235, 
                              52.100775, 52.0748232, 52.0102848, 52.089504, 52.0164101027027, 
                              52.0083622, 52.0895125619048, 52.038528, 52.1041174, 52.0963176, 
                              52.0153379, 52.1024123, 52.0895413, 52.0931169, 52.015444875, 
                              52.0386511, 52.0516474, 52.0895002451613, 52.0215369, 52.0991255, 
                              52.0969218, 52.0893316, 52.066435, 52.0139796, 52.0981765, 52.0897099, 
                              52.0352992, 52.051733, 52.0530949, 52.08922647, 52.0107568, 52.0677648, 
                              52.0895214666667, 52.089504648, 52.100359, 52.09324255, 52.0620195, 
                              52.0466879, 52.017858, 52.0135695, 52.0254736, 52.0171776, 52.028283, 
                              52.0171794, 52.1010769, 52.0178973, 52.0924092, 52.0897434, 52.0505729, 
                              52.0291358, 52.0893442, 52.0086011, 52.0992469, 52.059978, 52.0897928, 
                              52.0892269041667, 52.1054074, 52.089251, 52.0526539, 52.0266356, 
                              52.0449901, 52.1051109, 52.0612085, 52.0897635, 52.0618627, 52.0894992, 
                              52.1059497, 52.015919275, 52.0114324, 52.0511659, 52.0892549333333, 
                              52.0235488, 52.0272761, 52.09694, 52.0521608, 52.0153993, 52.1060753, 
                              52.0175162), lon = c(4.8527414, 4.7043579, 5.1092251, 4.558967, 
                                                   4.4474887, 5.1093993, 5.10904667619048, 4.7878027, 4.8367076, 
                                                   4.95351535, 4.6931897, 4.567024, 5.105847, 4.6899796, 4.9029363, 
                                                   5.1110543, 4.96241255, 5.108432375, 4.9690878, 4.73307101555556, 
                                                   4.9939, 5.110891905, 4.6441234, 4.496551, 5.1049426, 4.5854403, 
                                                   4.3598875, 4.8104103, 4.9753691, 5.10843242142857, 4.4603587, 
                                                   4.9321842, 5.110488365, 4.624286, 4.6469115, 4.865213, 4.6431315, 
                                                   5.1097292516129, 5.10848633, 4.9263356, 4.3614653, 4.4245138, 
                                                   5.10908557, 5.1092372, 4.929797, 4.6298071, 4.8542748, 4.691945, 
                                                   4.9882762, 4.9068603, 4.3707472, 5.1076963, 4.71963735765766, 
                                                   5.11119456, 5.10973931428571, 5.1110549, 4.9540848, 4.6488539, 
                                                   5.1050478, 5.1110486, 5.10973009032258, 5.0053341, 4.8341014, 
                                                   4.4882284, 4.4640663, 4.9004724, 4.8388151, 5.10903851428571, 
                                                   5.10884365, 4.7520508, 4.4393094, 4.8644185, 5.10953031818182, 
                                                   5.10972941935484, 5.10973205714286, 5.10849255, 4.601082, 4.4515908, 
                                                   5.10973025806452, 4.74402028494624, 5.1111849, 4.676354, 4.99798836666667, 
                                                   4.7009575, 5.031704, 5.0167332, 4.4321563, 4.8230864, 5.10928108181818, 
                                                   5.1062712, 4.3758328, 4.7058636, 4.5995564, 5.10900178571429, 
                                                   5.10843218928571, 5.0200993, 5.0389258, 4.8472408, 5.1091899, 
                                                   5.10847912727273, 4.6946867, 4.5265032, 4.8282194, 4.4697327, 
                                                   4.5144658, 4.6157094, 4.4114789, 4.6803854, 4.8436428, 4.5977714, 
                                                   5.10959262727273, 4.8362508, 5.0669724, 5.10884347272727, 4.71276212882883, 
                                                   4.8951137, 4.5001568, 5.1084314, 5.0888354, 5.0070498, 5.0845948, 
                                                   5.1110552, 5.0356131, 4.878915, 4.6636436, 5.10892755, 4.71882850720721, 
                                                   4.65157, 5.10974052380952, 4.8371255, 4.9957208, 5.1016242, 4.673318, 
                                                   5.0145183, 5.1100309, 5.1067818, 4.73506618, 4.5281429, 4.4214528, 
                                                   5.10972954516129, 4.7963679, 5.0638705, 5.0952241, 5.1084839, 
                                                   4.3598875, 4.7581467, 5.0759648, 5.1094376, 4.829447, 4.4204181, 
                                                   4.4020959, 5.10884371, 4.6393904, 4.3563094, 5.109842, 5.109726984, 
                                                   5.0417278, 4.91819815, 4.3726141, 4.4892613, 4.6975655, 4.6697432, 
                                                   4.580841, 4.7058672, 4.8126885, 4.7058869, 4.9413298, 4.6971556, 
                                                   4.9156515, 5.109391, 4.4370594, 4.5661368, 5.10852764, 4.6569508, 
                                                   5.0618936, 4.3785312, 5.1092584, 5.108826925, 4.9785586, 5.10890992857143, 
                                                   4.8577536, 4.5762531, 4.5027296, 4.9817912, 4.3749189, 5.1093724, 
                                                   4.373075, 5.1097303, 4.9606803, 4.72708552222222, 4.6660585, 
                                                   4.4280254, 5.1089249, 4.5886027, 4.5736119, 5.0948948, 4.4141114, 
                                                   4.7804527, 4.97133625, 4.6823925), time = structure(c(1549542028, 
                                                                                                         1549541565, 1549542658, 1549541192, 1549540912, 1549542689, 1549542880, 
                                                                                                         1549541872, 1549541986, 1549542287, 1549541524, 1549541210, 1549542600, 
                                                                                                         1549541514, 1549542184, 1549543182, 1549542303, 1549543013, 1549542315, 
                                                                                                         1549541756, 1549542360, 1549543125, 1549541393, 1549541039, 1549542592, 
                                                                                                         1549541253, 1549540695, 1549541925, 1549542326, 1549543012, 1549540937, 
                                                                                                         1549542243, 1549543129, 1549541352, 1549541399, 1549542075, 1549541391, 
                                                                                                         1549542809, 1549543046, 1549542231, 1549540734, 1549540867, 1549543079, 
                                                                                                         1549542660, 1549542238, 1549541363, 1549542034, 1549541520, 1549542350, 
                                                                                                         1549542192, 1549540756, 1549542623, 1549541704, 1549543122, 1549542766, 
                                                                                                         1549543211, 1549542288, 1549541403, 1549542593, 1549543174, 1549542829, 
                                                                                                         1549542381, 1549541980, 1549541009, 1549540944, 1549542179, 1549541991, 
                                                                                                         1549542882, 1549542947, 1549541800, 1549540896, 1549542072, 1549542861, 
                                                                                                         1549542813, 1549542772, 1549543000, 1549541296, 1549540920, 1549542833, 
                                                                                                         1549541784, 1549543143, 1549541474, 1549542368, 1549541550, 1549542428, 
                                                                                                         1549542401, 1549540882, 1549541954, 1549542869, 1549542604, 1549540767, 
                                                                                                         1549541600, 1549541292, 1549542891, 1549543017, 1549542407, 1549542441, 
                                                                                                         1549542011, 1549542653, 1549543044, 1549541529, 1549541119, 1549541966, 
                                                                                                         1549540955, 1549541091, 1549541333, 1549540842, 1549541485, 1549542002, 
                                                                                                         1549541287, 1549542859, 1549541985, 1549542493, 1549542970, 1549541687, 
                                                                                                         1549542168, 1549541052, 1549543034, 1549542539, 1549542384, 1549542529, 
                                                                                                         1549543225, 1549542435, 1549542125, 1549541436, 1549543075, 1549541702, 
                                                                                                         1549541409, 1549542765, 1549541987, 1549542364, 1549542577, 1549541466, 
                                                                                                         1549542397, 1549542736, 1549542610, 1549541764, 1549541123, 1549540861, 
                                                                                                         1549542816, 1549541892, 1549542487, 1549542555, 1549543045, 1549540730, 
                                                                                                         1549541812, 1549542511, 1549542701, 1549541969, 1549540859, 1549540824, 
                                                                                                         1549542949, 1549541383, 1549540720, 1549542756, 1549542791, 1549542446, 
                                                                                                         1549542215, 1549540760, 1549541013, 1549541538, 1549541455, 1549541242, 
                                                                                                         1549541668, 1549541930, 1549541670, 1549542262, 1549541537, 1549542210, 
                                                                                                         1549542688, 1549540892, 1549541208, 1549543063, 1549541420, 1549542483, 
                                                                                                         1549540773, 1549542679, 1549542925, 1549542332, 1549542906, 1549542047, 
                                                                                                         1549541231, 1549541060, 1549542338, 1549540765, 1549542686, 1549540761, 
                                                                                                         1549542834, 1549542300, 1549541732, 1549541443, 1549540874, 1549542904, 
                                                                                                         1549541261, 1549541225, 1549542554, 1549540847, 1549541856, 1549542319, 
                                                                                                         1549541491), class = c("POSIXct", "POSIXt"), tzone = "Europe/Paris")), class = c("tbl_df", 
                                                                                                                                                                                          "tbl", "data.frame"), row.names = c(NA, -200L), .Names = c("lat", 
                                                                                                                                                                                                                                                     "lon", "time"))

加载地图作为背景


LAT1 = in(df3$lat)  -.07
LAT2 = max(df3$lat)  +.07
LON1 = min(df3$lon) -.07
LON2 = max(df3$lon) +.07

map_osm <- openmap(c(LAT2,LON1), c(LAT1,LON2), zoom = NULL,
                   type = "stamen-toner",
                   mergeTiles = TRUE,
                   minNumTiles=10)

map_osm <- openproj(map_osm, projection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

map_google_box <- make_bbox(lat = df3$lat, lon = df3$lon, f = 0.5)
map_google <- get_map(location = map_google_box)

动画


# this works
ani1 <- ggplot()+
  geom_point(data = df3,
             aes(y =lat, x= lon),
             size = 2,
             col  = 'red')+
  transition_time(time) +
  ease_aes('linear')+
  shadow_mark()+
  view_follow()

animate(ani1)


# this doesnt
ani2 <- ggmap(map_google) +
  geom_point(data = df3,
             aes(y =lat, x= lon),
             size = 2,
             col  = 'red')+
  transition_time(time) +
  ease_aes('linear')+
  shadow_mark()+
  view_follow()

animate(ani2)

# this doesnt either
ani3 <- autoplot(map_osm)  +
  geom_point(data = df3,
            aes(y =lat, x= lon),
            size = 2,
            col  = 'red')+
  transition_time(time) +
  ease_aes('linear')+
  shadow_mark()+
  view_follow()


animate(ani3)

4

0 回答 0