2017-04-04 164 views
0

我在使用ggmap時出現以下錯誤:Error in if (is.waive(data) || empty(data)) return(cbind(data, PANEL = integer(0))) : missing value where TRUE/FALSE needed 我一直在使用ggmap很長一段時間,但無法弄清楚問題所在 - 嘗試在不同版本的R(3.3.2,3.3.3)仍然遇到同樣的問題。用相同的空間範圍的情節確定...ggmap - 缺失值

玩具數據集

sub <- structure(list(Lat = c(49.3292885463864, 49.3316446084215, 
49.3300452342386, 49.3317620975044, 49.3304515659156, 49.3305117886863, 
49.3283736754004, 49.3307167462002, 49.3318995940679, 49.333169419547, 
49.3309562839252, 49.3317698899629, 49.3281374770165, 49.3316554590127, 
49.3326735200194, 49.331519408234, 49.3280156106529, 49.3291709916829, 
49.3328300103323, 49.3306140984074), Lon = c(-117.657207875892, 
-117.672957375133, -117.66331506511, -117.672862630678, -117.66304525751, 
-117.668207331581, -117.655158806988, -117.66229183045, -117.673965605927, 
-117.673707660621, -117.662873110863, -117.673069192809, -117.655568553898, 
-117.674182492008, -117.673907352374, -117.675914855, -117.65485127671, 
-117.657316414995, -117.671748537091, -117.662937333234), z = 
c(9.27369836928302, 2.39183027404169, -1.93395087707449, -3.18890166171755, 
-0.97968656067399, 2.2968631268102, 8.25209737911514, -1.44955530148785, 
-1.16576549902187, 0.341268832113262, -1.15519233610136, 10.2579242298728, 
-4.65813764430002, 0.301315621593428, 5.25169173852741, -5.37463429849591, 
4.70020657978266, -4.64139357200872, -1.38702225667279, 9.38668592801448)), 
.Names = c("Lat", "Lon", "z"), row.names = c(266748L, 266749L, 266750L, 
266756L, 266758L, 266760L, 266768L, 266770L, 266771L, 266772L, 266778L, 
266782L, 266783L, 266784L, 266787L, 266791L, 266792L, 266796L, 
266801L, 266802L), class = "data.frame") 

代碼

library(ggmap) 
library(ggplot2) 

prep <- get_googlemap(center = c(-117.660, 49.329), zoom = 14, 
maptype = 'satellite', scale = 2) 

map <- ggmap(prep, size = c(100, 200), 
    extent='device', darken = 0, legend = "bottom", 
    base_layer = ggplot(data = sub, aes(x = Lon, y = Lat))) 

map 
map + geom_point(data = sub, aes(x = Lon, y = Lat, colour = z), size = 1) 

編輯

我需要使用方面,這就是爲什麼base_layer調用其他數據集在那兒。

回答

1

爲什麼base_layer位?

也能正常工作對我來說:

map <- ggmap(prep, size = c(100, 200), 
      extent='device', darken = 0, legend = "bottom") 
map + geom_point(data = sub, aes(x = Lon, y = Lat, colour = z), size = 1) 

更新:

工程與facet_wrap,太!

sub$facet <- sample(x = 1:4, size = nrow(sub), replace = TRUE) 

map2 <- ggmap(ggmap = get_googlemap(maptype = 'satellite', 
            center = c(-117.660, 49.329), 
            zoom = 14)) + 
    geom_point(data = sub, aes(x = Lon, y = Lat, colour = z), size = 1) + 
    facet_wrap(~ facet, nrow = 2) 
+0

需要使用方面,這需要base_layer,據我瞭解... – user2602640

+0

看到我更新的答案 – JanLauGe

+0

不知何時變了。 ggmap有助於確保它聽起來像需要base_layer進行分層!這只是星期二,我已經學到了一些東西;-) http://127.0.0.1:22682/library/ggmap/html/ggmap.html – user2602640