本文介绍了R - 在使用注释时将图例添加到ggmap(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! FYI:我对ggplot2和ggmap相当陌生,所以我对这些草率的代码表示歉意,但这是我能够绘制每组具有自己颜色的点组的集合的唯一方式。我的操作系统是Ubuntu的。 我试图添加一个图例,一个ggmap对象,特别是一个带连续渐变过渡颜色的图例。有什么建议?我已经尝试了ggmap中的图例属性,但它似乎没有工作。下面是我到目前为止。 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]]),color = 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]]),color = colorval [i],cex = cexval,pch = pchval) i = topnum; (SYDmap 解决方案不使用注释,下面是一个使用 geom_point 。几乎所有的geom都可以添加到ggmap对象中,因为它会被添加到ggplot对象中。由于 Size (请参阅下面的 df 数据框的内容)在调用 geom_point ,图例自动生成。 图书馆(ggmap) #获取地图 - 堪培拉地图可以做 ACTmap = get_map(c(149.1,-35.325),zoom = 12,source =google,maptype =roadmap) #数据帧的坐标和坐标以及变量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)) #绘制地图 ACTmap = ggmap(ACTmap) #添加 ACTmap = ACTmap + geom_point(data = df,aes(x = lon,y = lat,color = Size), alpha = 0.6,size = 10) #更改图例 ACTmap + scale_colour_continuous(low =red,high =blue,space =Lab,guide =colorbar) pre> FYI: I'm fairly new to ggplot2 and ggmap so I apologize for the sloppy code but it is the only way I've been able to plot sets of groups of points where each group has it's own color. Also my os is ubuntu.I'm trying to add a legend, to a ggmap object and in particular a legend with a continuos gradient transitioning the colors. Any advice? I've tried the legend attribute in ggmap but it doesn't seem to be working. Below is what I have so far.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") 解决方案 Instead of using annotate, here's a method that adds a point layer using geom_point. Almost any geom can be added to a ggmap object as it would be added to a ggplot object. Because Size (see the contents of the df data frame below) is a colour aesthetic in the call to geom_point, the legend is generated automatically. library(ggmap)# Get a map - A map of Canberra will doACTmap = get_map(c(149.1, -35.325), zoom = 12, source = "google", maptype = "roadmap")# A data frame of lon and lat coordinates and the variable Sizedf = 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 mapACTmap = ggmap(ACTmap)# Add the pointsACTmap = ACTmap + geom_point(data = df, aes(x = lon, y = lat, colour = Size), alpha = 0.6, size = 10)# Change the legendACTmap + scale_colour_continuous(low = "red", high = "blue", space = "Lab", guide = "colorbar") 这篇关于R - 在使用注释时将图例添加到ggmap(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 20:45