2

I was trying to label points using name on the map conditionally for a city.

Data

name |s  Latitude |s Longitude |s Frequency

Pelham Bay Park | 40.865556 | -73.808333 | 32

Greenbelt        | 40.58846 | -74.139073  | 10

Van Cortlandt Park | 40.8978 | -73.8839 | 3

Flushing Meadows–Corona Park | 40.745833 | -73.844722 | 5

Central Park | 40.783333 | -73.966667 |22

Code

library(ggmap)
town <- get_map(location = "New York", zoom = 10)

mapPoints <- ggmap(town) + 
  geom_point(aes(x = work_count$Longitude, y = work_count$Latitude, size = work_count$Frequency), data = work_count, alpha = .5) +
geom_text(aes(label=ifelse(work_count$Frequency>quart(work_count$Frequency)[2],as.character(work_count$name),'')),hjust=0,vjust=0)

quart <- function(x) {
  x <- sort(x)
  n <- length(x)
  m <- (n+1)/2
  if (floor(m) != m) {
     l <- m-1/2; u <- m+1/2
  } else {
     l <- m-1; u <- m+1
   }
   c(Q1=median(x[1:l]), Q3=median(x[u:n]))
 }
mapPoints

I am getting the following error: Error: Aesthetics must be either length 1 or the same as the data (5): label, x, y Any help is appreciated

bob jones
  • 23
  • 1
  • 5
  • It easier to help you with plotting questions if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run your code to see what's wrong and test possible solutions. – MrFlick Aug 12 '16 at 19:38
  • You might want to pre-process the labels rather than calculating them on the fly in `geom_text`, i.e. `work_count$labels <- ifelse(work_count$Frequency>quart(work_count$Frequency)[2],as.character(work_count$name),''))`, then pass that into `geom_text`. – Weihuang Wong Aug 12 '16 at 19:39
  • @Weihuang Wong I have done and got a column where only entries that meet my criteria exist and rest are empty. And I passed the work_count$labels to the label field in geom_text, but couldn't get the output – bob jones Aug 12 '16 at 19:50
  • Please see comment above about providing a reproducible example. E.g. show the code you used to create `town`, and do `dput(head(work_count, 20))` and paste the output into your question to show us the first few rows of your data. – Weihuang Wong Aug 12 '16 at 20:07
  • Since data argument in geom_XXX, you don't need to specify the data frame in `aes`. It might not fix your problem, but it will make your code much cleaner. dput is much more useful than a printout of your table – Richard Telford Aug 12 '16 at 20:56
  • Your function quart looks like a complicated alternative to `quantile` – Richard Telford Aug 12 '16 at 21:07
  • I haven't tested this but I think `geom_text()` needs the data frame named: `data = work_count`, as you did for `geom_point()` – Sandy Muspratt Aug 12 '16 at 21:10
  • Also, `geom_text` doesn't know where to put the labels. – Sandy Muspratt Aug 12 '16 at 21:46
  • Some how, I figured it out the way. work_count$labels <- ifelse(work_count$Frequency >(quart(work_count$Frequency)[2] + 1.5*IQR(work_count$Frequency)),as.character(work_count$name),'') mapPoints <- ggmap(town) + geom_point(aes(x = work_count$Longitude, y = work_count$Latitude, size = work_count$Frequency), data = work_count, alpha = .5) + geom_text(data = work_count, aes(x = longitude, y = latitude, label = work_count$labels), size = 3, colour = "red")' I didn't get text and label closely. What is the suggestible value for hjust and vjust. – bob jones Aug 12 '16 at 21:59
  • This one is bugging me a lot. It seemed like it worked and again its throwing me Error: Aesthetics must be either length 1 or the same as the data (5): label, x, y – bob jones Aug 12 '16 at 22:39

1 Answers1

2

This seems to work.

work_count$lab <- ifelse(work_count$Frequency>quart(work_count$Frequency)[2],
  as.character(work_count$name), '')

mapPoints <- ggmap(town, 
                   base_layer = ggplot(work_count, 
                     aes(x = Longitude, y = Latitude))) +
  geom_point(aes(size = Frequency), 
             alpha = .5) +
  geom_text(aes(label = labels), 
            vjust = 0,
            hjust = 0)
mapPoints

enter image description here

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
  • Thanks. 'ggmap(town, base_layer = ggplot(data=work_count, aes(x = Longitude, y = Latitude, label=labels))) + geom_point(fill = "black", alpha =0.8, aes(x = work_count$Longitude, y = work_count$Latitude, size = work_count$Frequency), shape = 21) + geom_text()' got answer for me too. Do you know how to fit zoom dynamically for town variable if we are working with several cities and one input at once? – bob jones Aug 12 '16 at 23:31
  • Indeed, it's much neater to specify `data` and `aes()` in `ggmap(..., base_layer=ggplot(...))`. I've edited my answer to reflect that. (Also, as other people have mentioned above, you don't need all those `work_count$...` in your geoms once you've specified the data in either `ggplot(data=...)` or in `geom_*(data=...)`.) Your question about dynamic zoom is a separate question by itself! – Weihuang Wong Aug 12 '16 at 23:52
  • How can we label only the points which satisfies the condition with other color compared to rest in this case. In this case for Pelham Bay Park. – bob jones Aug 16 '16 at 21:30