cst*_*sta 3 plot r legend ggplot2 ggmap
仅供参考:我对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")
Run Code Online (Sandbox Code Playgroud)
而不是使用annotate
,这是一个使用添加点图层的方法geom_point
.几乎任何geom都可以添加到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")
Run Code Online (Sandbox Code Playgroud)