How to change ggplot legend labels and names with two layers?
Asked Answered
T

1

5

I am plotting the longitude and latitude coordinates of two different data frames in São Paulo map using ggmap and ggplot packages and want to label manually each legend layer:

update: I edited my code below to become fully reproducible (I was using the geocode function instead of get_map).

update: I would like to do this without combining the data frames.

require(ggmap)

sp <- get_map('sao paulo', zoom=11, color='bw')
restaurants <- data.frame(lon=c(-46.73147, -46.65389, -46.67610), 
                          lat=c(-23.57462, -23.56360, -23.53748))
suppliers <- data.frame(lon=c(-46.70819,-46.68155, -46.74376), 
                        lat=c(-23.53382, -23.53942, -23.56630))
ggmap(sp)+geom_point(data=restaurants, aes(x=lon, y=lat),color='blue',size=4)+geom_point(data=suppliers, aes(x=lon, y=lat), color='red', size=4)

enter image description here

I have looked to several questions and tried different ways without success. Does anyone know how can I insert legend and label the blue points as restaurants and the red points as suppliers?

Treytri answered 22/6, 2012 at 15:14 Comment(3)
Combine data frames. Add third variable to indicate rest/supp. Map colour to third variable.Institutive
I should add that I was going to write a complete answer, but your code isn't reproducible.Institutive
Sorry @Joran. There was an error in my code, but I already corrected it. It should be fully reproducible now. Thanks.Treytri
I
8

Now that your code is reproducible (thanks!):

dat <- rbind(restaurants,suppliers)
dat$grp <- rep(c('Restaurants','Suppliers'),each = 3)

ggmap(sp) + 
    geom_point(data=dat, aes(x=lon, y=lat,colour = grp),size = 4) + 
    scale_colour_manual(values = c('red','blue'))

enter image description here

Institutive answered 22/6, 2012 at 16:50 Comment(5)
Thanks! This solution really do what I asked. However, I really appreciate a solution without combining the data frames. As ggplot2 allows the overlapping of different layers, it is probably possible to manually change the labels of both legends.Treytri
@DaviMoreira That's not really the way ggplot works. In general, one layer:one legend. Combining the data frames is how you do it in ggplot.Institutive
You're right. But what I had in mind is something along the lines of the way that Hadley plotted [link]stat.columbia.edu/~gelman/bayescomputation/Wickham2010.pdf) the famous Minard Map. There are two data frames (troops and cities) and they aren't merged, because they're organized in different ways (at least I guess that's the reason). And it happened that I may have this problem in my data set, since data about restaurants are organized in a different way than data about suppliers. It may be possible to rearrange both data sets to combine them, but I feel it'll be hard to do so.Treytri
@DaviMoreira That example involves two data sets, but they absolutely do not share a common legend. In fact, the cities data does not produce any legend at all for that data. You can't do separate layer calls and have one legend for the data in both sets.Institutive
Really, since nothing is ever impossible, I should say that combining your data frames will be much less awkward than forcing ggplot to behave in a way that it wasn't intended. R is designed to make merging/combining data easy and convenient. Work with the tools you have, not against them.Institutive

© 2022 - 2024 — McMap. All rights reserved.