【问题标题】:R - adding legend to ggmap (ggplot2) while using annotateR - 在使用注释时向 ggmap (ggplot2) 添加图例
【发布时间】:2012-06-06 16:17:34
【问题描述】:

仅供参考:我对 ggplot2 和 ggmap 还很陌生,所以我为草率的代码道歉,但这是我能够绘制一组点组的唯一方法,每个组都有自己的颜色。我的操作系统也是ubuntu。

我正在尝试将图例添加到 ggmap 对象,特别是具有过渡颜色的连续渐变的图例。有什么建议吗?我已经尝试过 ggmap 中的 legend 属性,但它似乎不起作用。以下是我目前所拥有的。

syd = get_map(location = center, zoom = zoom, maptype = type,color = "bw")

(SYDmap = ggmap(syd, extent = "panel",legend="right")+ annotate('point',x=lng[[1]],xend=max(lng[[1]]),y=lat[[1]],yend=max(lat[[1]]),colour=colorval[1],cex=cexval,pch=pchval))

for(i in 2:(topnum - 1))
  SYDmap<- SYDmap + annotate('point',x=lng[[i]],xend=max(lng[[i]]),y=lat[[i]],yend=max(lat[[i]]),colour=colorval[i],cex=cexval,pch=pchval)

i=topnum;  (SYDmap <-   SYDmap + annotate('point',x=lng[[i]],xend=max(lng[[i]]),y=lat[[i]],yend=max(lat[[i]]),colour=colorval[i],cex=cexval,pch=pchval)) + guides(fill = "colourbar")

【问题讨论】:

标签: r plot ggplot2 legend ggmap


【解决方案1】:

这里没有使用annotate,而是使用geom_point 添加点层的方法。几乎任何几何图形都可以添加到 ggmap 对象中,就像它会添加到 ggplot 对象中一样。因为Size(参见下面df 数据框的内容)是对geom_point 的调用中的颜色美学,所以会自动生成图例。

library(ggmap)

# Get a map - A map of Canberra will do
ACTmap = get_map(c(149.1, -35.325), zoom = 12, source = "google", maptype = "roadmap")

# A data frame of lon and lat coordinates and the variable Size
df = data.frame(lon = c(149.0307, 149.1326, 149.089, 149.048, 149.0965),
            lat = c(-35.3892, -35.28225, -35.34005, -35.34857, -35.34833),
            Size = c(1,2,3,4,5))

# Draw the map
ACTmap = ggmap(ACTmap)

# Add the points
ACTmap = ACTmap + geom_point(data = df, aes(x = lon, y = lat, colour = Size), 
      alpha = 0.6,  size = 10)

# Change the legend
ACTmap + scale_colour_continuous(low = "red", high = "blue", space = "Lab", guide = "colorbar")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 2016-06-22
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2016-03-12
    相关资源
    最近更新 更多