使用注释时的ggmap和错误(geom = " text ")

时间:2021-07-29 07:00:44

My data frame of traffic counts at various locations in Princeton.

我的交通数据在普林斯顿的各个地方都有。

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

Earlier questions 1 conflict with annotate in other package are not useful 2 solution to create another data frame.

前面的问题1与其他包中的注释冲突不是创建另一个数据框架的有用解决方案。

EDIT per comment -- the error message I get:

编辑每条评论——我得到的错误消息:

Error: 'x' and 'units' must have length > 0

Here is the plot, which shows the boundaries on the axes.

这是图,它显示了坐标轴上的边界。

使用注释时的ggmap和错误(geom = " text ")

1 个解决方案

#1


3  

After looking into this case, I think Error: 'x' and 'units' must have length > 0 indicates that the annotation point does not exist in the map.

在研究这个例子之后,我认为Error: 'x'和'units'必须有长度> 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

If you look at lon and lat values above, -74.7 and 40.4 are the values for bbox. But, seeing the error message, the bbox values may not be included. If so and if you want to have your annotation point at x = -74.7, y = 40.4, you need another approach. My approach was to get a map with a small zoom value (e.g., zoom = 12) and trim the map with scale_x_continuous and scale_y_continuous. In this way, I made sure that the annotation point stays in the map. The following map is missing one data point, by the way. If you want to have it in your map, you need to play with lon/lat values.

如果你看上面的lon和lat值,-74.7和40.4是bbox的值。但是,如果看到错误消息,可能不包含bbox值。如果是这样,并且如果您想要在x = -74.7, y = 40.4处拥有注释点,则需要另一种方法。我的方法是获得一个具有小缩放值的映射(例如,zoom = 12),并使用scale_x_continuous和scale_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))

使用注释时的ggmap和错误(geom = " text ")

#1


3  

After looking into this case, I think Error: 'x' and 'units' must have length > 0 indicates that the annotation point does not exist in the map.

在研究这个例子之后,我认为Error: 'x'和'units'必须有长度> 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

If you look at lon and lat values above, -74.7 and 40.4 are the values for bbox. But, seeing the error message, the bbox values may not be included. If so and if you want to have your annotation point at x = -74.7, y = 40.4, you need another approach. My approach was to get a map with a small zoom value (e.g., zoom = 12) and trim the map with scale_x_continuous and scale_y_continuous. In this way, I made sure that the annotation point stays in the map. The following map is missing one data point, by the way. If you want to have it in your map, you need to play with lon/lat values.

如果你看上面的lon和lat值,-74.7和40.4是bbox的值。但是,如果看到错误消息,可能不包含bbox值。如果是这样,并且如果您想要在x = -74.7, y = 40.4处拥有注释点,则需要另一种方法。我的方法是获得一个具有小缩放值的映射(例如,zoom = 12),并使用scale_x_continuous和scale_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))

使用注释时的ggmap和错误(geom = " text ")