繁体   English   中英

ggplot2在图例中添加额外信息

[英]ggplot2 adding extra info in legend

我有加拿大的地图

library(ggmap)
mp = get_map(location = "Canada", maptype="satellite", color="color")

然后我想在地图上绘制14个点:

p = ggmap(mp) + 
    geom_point(data=GPScor, x=lon, y = lat, col="red", size =2.1) + 

那我想给你图上的点编号:

geom_text(data=GPScor, aes(x=decLon, y=decLat,label=lbl),
    adj=2,offset=1, color="white", size=4, hjust=4) 

我找不到如何创建图例的方式,该图例将列出带有相应点名称的数字:类似

1: "name of point1"
2: "name of point2 "   

抱歉,我最初误解了这个问题,您应该在geom_pointaes添加名称。

dat<-data.frame(name=c("a","b","c"), x=1:3, y=1:3)

ggplot(dat, aes(x=x, y=y))+
  geom_point(aes(shape=paste(1:3,": point",name)))+
  geom_text(aes(x=x,y=y+0.07), label=1:3)+
  labs(shape="locations")

如果您实际上不希望它们以不同的形状显示,则只需使用scale_shape_manual覆盖它们并设置一个任意值即可。

  scale_shape_manual(values=rep(1,3))

下面放在一起

dat<-data.frame(name=c("a","b","c"), x=1:3, y=1:3)

ggplot(dat, aes(x=x, y=y))+
  geom_point(aes(shape=paste(1:3,": point",name)))+
  geom_text(aes(x=x,y=y+0.07), label=1:3)+
  scale_shape_manual(values=rep(1,3))+
  labs(shape="locations")

在此处输入图片说明

这是显式使用ggmap的示例

mp <- get_map(location = "Canada", maptype="satellite", color="color")
dat<-data.frame(lon=c(-106.6, -106.4), lat=c(56.0, 56.2), names=c("1","2"))
  ggmap(mp)+
  geom_point(data=dat, aes(x=lon, y = lat, 
             color=paste(seq_along(dat$lon), ": location", names),
             shape=paste(seq_along(dat$lon), ": location", names)))+
  scale_shape_manual(values=rep(1,nrow(dat)))+
  labs(shape="locations", color="locations")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM