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

烂漫一生 提交于 2020-01-01 12:06:08

问题


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)

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?


回答1:


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'))



来源:https://stackoverflow.com/questions/11159072/how-to-change-ggplot-legend-labels-and-names-with-two-layers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!