使用annotate(geom="text")时出现ggmap错误

3

我在普林斯顿的各个位置记录了交通计数的数据框。

# dput(count)
structure(list(intersection = structure(c(11L, 9L, 10L, 12L, 
6L, 3L, 7L, 2L, 4L, 1L, 5L, 8L), .Label = c("CherryHillat206", 
"ElmatRidgeRd", "FacultyatHarrison", "HarrisonatLake", "HarrisonbetwHamilton", 
"MerceratLoversLane", "ProvinceLineatMercer", "RiverRdat27", 
"Rt 27 Bank", "Rt. 27 River Rd", "US206 Cambelton", "US206Princeton Ave."
), class = "factor"), traffic = c(19352, 18697, 12493, 21554, 
10871, 13310, 7283, 11408, 12055, 6415, 14100, 5739), lat = c(40.3475418, 
40.3487282, 40.3711205, 40.3909988, 40.3403702, 40.3434601, 40.343689, 
40.3440514, 40.3454819, 40.3627014, 40.3658734, 40.3738098), 
lon = c(-74.6711197, -74.6630707, -74.62323, -74.6541214, 
-74.6720123, -74.647049, -74.7051392, -74.7334671, -74.6390533, 
-74.6648483, -74.6596518, -74.6207962), class = structure(c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("primary", 
"secondary"), class = "factor")), .Names = c("intersection", 
"traffic", "lat", "lon", "class"), row.names = c(NA, -12L), class = "data.frame")

library(ggplot2)
library(ggmap)

map <- get_map(location = 'princeton', zoom = 13, maptype = "road")

ggmap(map) +
geom_point(data = count, aes(x = lon, y = lat)) + 
annotate("text", x = -74.7, y = 40.4, label = "TEST anno")

早期的问题1与另一个包中的annotate发生冲突的解决方法并不实用.另一种解决方法是创建另一个数据框
根据评论进行编辑--我收到的错误信息:
Error: 'x' and 'units' must have length > 0

这里是图表,展示了坐标轴的边界。 enter image description here

你是否遇到了这个错误:错误:ggplot2不知道如何处理类函数的数据 - rmuc8
@rmuc8:不,我收到了错误信息:“x”和“units”的长度必须大于0。 - lawyeR
@jazzuro,感谢您的努力。我已经放入了错误消息和图形的外观,其中包括我随机尝试注释文本的位置。我会看看“count”是否是罪魁祸首。 - lawyeR
@jazzurro -- "count"被全部清除了罪名 :). 我把它叫做"df",但是收到了相同的错误信息。 - lawyeR
1
如果您缩小了缩放值,我认为您将能够看到文本。map2 <- get_map(location = 'princeton', zoom = 12, maptype = "road")。至少,我能够按照您的意图看到文本。鉴于此,我仍然认为您尝试注释的点在地图中不存在。 - jazzurro
显示剩余7条评论
1个回答

3

经过调查,我认为Error: 'x' and 'units' must have length > 0表示注释点在地图中不存在。

str(map)
# chr [1:1280, 1:1280] "#F0EDE4" "#F0EDE4" "#F0EDE4" "#F0EDE4" ...
# - attr(*, "class")= chr [1:2] "ggmap" "raster"
# - attr(*, "bb")='data.frame': 1 obs. of  4 variables:
#  ..$ ll.lat: num 40.3
#  ..$ ll.lon: num -74.7
#  ..$ ur.lat: num 40.4
#  ..$ ur.lon: num -74.6

如果您查看上面的lon和lat值,-74.7和40.4是bbox的值。但是,看到错误消息,bbox值可能不包括在内。如果是这样,并且您希望将注释点放置在x = -74.7,y = 40.4处,则需要另一种方法。我的方法是使用小的缩放值(例如,缩放=12)获取地图,并使用scale_x_continuousscale_y_continuous修剪地图。通过这种方式,我确保注释点留在地图上。顺便说一下,以下地图缺少一个数据点。如果您想在地图中添加它,您需要调整lon / lat值。
map2 <- get_map(location = 'princeton', zoom = 12, maptype = "road")

ggmap(map2) +
geom_point(data = mydf, aes(x = lon, y = lat)) + 
annotate("text", x = -74.7, y = 40.4, label = "TEST anno") +
scale_x_continuous(limits = c(-74.71, -74.6)) +
scale_y_continuous(limits = c(40.3, 40.4))

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接