【问题标题】:ggplot2 adding extra info in legendggplot2 在图例中添加额外信息
【发布时间】:2014-06-06 17:39:24
【问题描述】:

我有一张加拿大地图

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 "   

【问题讨论】:

标签: r ggplot2 legend


【解决方案1】:

对不起,我最初误读了这个问题 您应该在aes 中为您的geom_point 添加名称。

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

【讨论】:

  • 只需获取相关的geom_point 部分并按其他方式添加
  • 你似乎使用geom_point 很好,不是吗?还是您的问题是 geom__point 也有错误?
  • 图书馆(ggmap) mp = get_map(location = "Canada", maptype="satellite", color="color") dat
  • 您不能只将其添加到您的示例中。我上面的简单示例只是为了展示应该如何使用 aes(shape),您需要根据需要为您的示例进行调整....
  • 明白了。将在我的计算机上下载 ggmap 并发送解决方案。我在工作,所以以前不能这样做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 2012-07-27
  • 2017-05-26
  • 2019-11-10
相关资源
最近更新 更多