【问题标题】:ggmap and error when using annotate(geom = "text")使用注释时的ggmap和错误(geom =“text”)
【发布时间】:2015-03-18 09:06:20
【问题描述】:

我的普林斯顿不同地点的流量数据框。

# 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 conflict with annotate in other package 没用2 solution to create another data frame

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

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

这是显示坐标轴边界的图。

【问题讨论】:

  • 您是否收到此错误:Error: ggplot2 doesn't know how to deal with data of class function
  • @rmuc8: 不,我得到错误:'x' and 'units' must have length > 0
  • 使用您的数据 c&p,我收到上面发布的错误。但是,您可能希望在帖子中包含错误消息。
  • 我认为你的数据名称是Error: ggplot2 doesn't know how to deal with data of class function的原因。 count 是 plyr/dplyr 中的一个函数。一旦我将数据的名称更改为 mydf,我就有了Error: 'x' and 'units' must have length &gt; 0
  • 如果你有一个较小的缩放值,我想你会看到文字。 map2 &lt;- get_map(location = 'princeton', zoom = 12, maptype = "road")。至少,我看到了你想要的文字。鉴于我仍然认为map 中不存在您尝试注释的点。

标签: r ggplot2 ggmap annotate


【解决方案1】:

看了这个案例,我觉得Error: 'x' and 'units' must have length &gt; 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))

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2012-06-15
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多