如何更改ggplot图例标签和名称与两层? [英] How to change ggplot legend labels and names with two layers?

查看:2845
本文介绍了如何更改ggplot图例标签和名称与两层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggmap和ggplot软件包绘制圣保罗地图中两个不同数据帧的经度和纬度坐标,并且想要手动为每个图例层添加标签:

更新:我编辑了下面的代码以完全重现(我使用的是geocode函数,而不是get_map)。



update:我想在不组合数据框。
$ b $ pre $ require(ggmap)

sp < - get_map('sao paulo',zoom = 11,颜色='bw')
餐厅< - 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)



我看过几个问题,并尝试了不同的方式没有成功。有谁知道我该如何插入图例并将蓝点标记为餐馆和红点作为供应商?

现在你的代码是可重现的(谢谢!):

$ $ p $ dat < - rbind(餐厅,供应商)
dat $ grp< ; - rep(c('Restaurants','Suppliers'),each = 3)

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


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?

解决方案

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

这篇关于如何更改ggplot图例标签和名称与两层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆