2

I am working with ggmap. the goal is to plot coordinate points on the map and label the points with their names. I have data frame with name, longitude and latitude.

The data looks like:

df <- structure(list(Station.Area = c("Balbriggan", "Blanchardstown", 
"Dolphins Barn", "Donnybrook", "Dun Laoghaire", "Finglas"), Latitude = c(53.608319, 
53.386813, 53.333532, 53.319259, 53.294396, 53.390325), Longitude = c(-6.18208, 
-6.377197, -6.29146, -6.232017, -6.133867, -6.298401)), .Names =c("Station.Area","Latitude", "Longitude"), row.names = c(NA, 6L), class = "data.frame")

The code I wrote is as below:

library(ggmap)
library(ggplot2)

dub_map <- get_map(location = "Dublin", zoom = "auto", scale="auto", crop = TRUE, maptype = "hybrid")

ggmap(dub_map) +`
    geom_point(data = df, aes(x = Longitude, y = Latitude, 
              fill = "green", alpha =` `0.8, size = 5, shape = 21)) +`
guides(fill=FALSE, alpha=FALSE, size=FALSE)+
geom_text(label=df$Station.Area)+
scale_shape_identity()

But i am getting

Error: Aesthetics must be either length 1 or the same as the data (4): label

I have tried to put various aesthetics in geom_text like size,color,x & Y but it still gives out same error.

Am i doing it correctly for my goal? Please help.

Getting this without geom_text now I just want to label the points

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
Sharvil Raval
  • 29
  • 1
  • 1
  • 5
  • I wonder if you want `geom_text(aes(label=Station.Area))` . Also dont size, alpha etc like that in the `aes` - do it outside – user20650 Jun 12 '16 at 19:55
  • yea i want to use the same but when i run the code without df$ it doesn't recognize the column. – Sharvil Raval Jun 12 '16 at 19:58
  • okay, can you share some of your data?? If so, can you edit your question with th results of `dput(head(df))` – user20650 Jun 12 '16 at 20:01
  • Station Area Latitude Longitude Balbriggan 53.608319 -6.18208 Blanchardstown 53.386813 -6.377197 Dolphins Barn 53.333532 -6.29146 Donnybrook 53.319259 -6.232017 Dun Laoghaire 53.294396 -6.133867 Finglas 53.390325 -6.298401 Kilbarrack 53.383747 -6.15254 North Strand 53.358192 -6.240886 Phibsborough 53.359906 -6.2724 Rathfarnham 53.300498 -6.283854 Skerries 53.580552 -6.107878 Swords 53.455747 -6.219741 Tallaght 53.288804 -6.355706 Tara St 53.3473 -6.2549 – Sharvil Raval Jun 12 '16 at 20:03
  • Sorry the data was in excel i just copied and pasted. Basically there are three columns Station Area, Latitude and Longitude. So if you can please read it like that – Sharvil Raval Jun 12 '16 at 20:05
  • Btw I didn't understand " can you edit your question with th results of dput(head(df))". – Sharvil Raval Jun 12 '16 at 20:09
  • by edit, I mean you click the edit button at the bottom left of your question, and paste in the results of `dput(head(df))` (which you type into your R session) – user20650 Jun 12 '16 at 20:12
  • that worked!!! thanks!!! – Sharvil Raval Jun 12 '16 at 20:16
  • Actually, this may be better... `ggmap(dub_map, base_layer = ggplot(data=df, aes(x = Longitude, y = Latitude, label=Station.Area))) + geom_point(fill = "green", alpha =0.8, size = 5, shape = 21) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_text() + scale_shape_identity()` to save passing the data to each geom – user20650 Jun 12 '16 at 20:16
  • 1
    perfect!! thanks again!! – Sharvil Raval Jun 12 '16 at 20:24

1 Answers1

3

There are a couple of things not quite right in your code.

For the geom_point you only want the x and y in your aes. The other arguments should be outside, giving

geom_point(data = df, aes(x = Longitude, y = Latitude), 
                  fill = "green", alpha =0.8, size = 5, shape = 21)

Also the label for the geom_text should be inside aes. However, as there is no data, x or y at a higher level, then geom_text will not find the label variable or the positions of where to place the labels. So you also need to include these in the call

geom_text(data=df, aes(x = Longitude, y = Latitude, label=Station.Area))

However, you can omit some of this repetition by using the base_layer argument of ggmap:

ggmap(dub_map, 
      base_layer = ggplot(data=df, aes(x = Longitude, 
                                       y = Latitude, 
                                       label=Station.Area))) +
      geom_point(fill = "green", alpha =0.8, size = 5, shape = 21) +
      geom_text() 
user20650
  • 24,654
  • 5
  • 56
  • 91