3

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.

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.

enter image description here

Community
  • 1
  • 1
lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • Do you get this error: `Error: ggplot2 doesn't know how to deal with data of class function` – rmuc8 Mar 17 '15 at 14:18
  • @rmuc8: no, I get Error: 'x' and 'units' must have length > 0 – lawyeR Mar 17 '15 at 14:22
  • using your data c&p, I get the error posted above. However, you might want to include your error message in your post. – rmuc8 Mar 17 '15 at 14:24
  • I think the name of your data is the cause of `Error: ggplot2 doesn't know how to deal with data of class function`. `count` is a function in plyr/dplyr. Once I changed the name of the data to mydf, I had `Error: 'x' and 'units' must have length > 0`. – jazzurro Mar 17 '15 at 14:28
  • I have been playing with the code. It seems to me that your lon value does not exist in the map although str(map) indicates that -74.7 is within bbox. That is probably why I see these warning messages `Warning messages:1: Removed 1 rows containing missing values (geom_point). 2: Removed 1 rows containing missing values (geom_text).` I can see a text with the following. `ggmap(map) + geom_point(data = mydf, aes(x = lon, y = lat)) + annotate("text", x = -74.67, y = 40.36, label = "TEST anno")`. x value is well in bbox in this case. – jazzurro Mar 17 '15 at 14:40
  • @jazzuro, thanks for your efforts. I put in the error message and what the plot looks like, which does include where I randomly tried to annotate text. I will see if "count" is the culprit. – lawyeR Mar 17 '15 at 14:47
  • @jazzurro -- "count" was acquitted of all charges :). I called it "df" and got the same error message. – lawyeR Mar 17 '15 at 14:48
  • 1
    If you have a smaller zoom value, I think you will see the text. `map2 <- get_map(location = 'princeton', zoom = 12, maptype = "road")`. At least, I see the text as you intended. Given that I still think that the point you tried to annotate does not exist in `map`. – jazzurro Mar 17 '15 at 16:08
  • @jazzurro: Thank you very much. You are right that zoom 12, showing a larger bbox, lets the annotation appear (plus solved my other mystery of a "missing point" that was obviously present). Do you want to submit an answer so I can accept it? For others' benefit, you could xxplain that the error means in this case "Annotation coordinates are outside your chosen map size, as determined by zoom." Or do I uptick your comments? – lawyeR Mar 17 '15 at 16:32
  • OK, I will post what I have tried. – jazzurro Mar 17 '15 at 23:41
  • One more thing. When you have missing-points error messages, that means your data points are out of bbox. This can be a cool thing when you want to plot data points within a certain area; you do not have to filter out data points before you draw a map. ggmap is automatically filtering data points outside of the bbox. If you want to plot all data points, make sure that you do not see the missing point error message. – jazzurro Mar 18 '15 at 01:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73212/discussion-between-lawyer-and-jazzurro). – lawyeR Mar 18 '15 at 01:11

1 Answers1

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.

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.

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

jazzurro
  • 23,179
  • 35
  • 66
  • 76