将图例添加到ggmap [英] Add legend to ggmap

查看:163
本文介绍了将图例添加到ggmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向R中的ggmap包生成的绘图中添加图例.我正在使用的数据集是

I am trying to add a legend to a plot generated by ggmap package in R. The dataset I am working with is

    Latitude  Longitude  amount
    61.37072 -152.40442  436774
    32.80667  -86.79113 3921030
    34.96970  -92.37312 1115087
    33.72976 -111.43122 5068957

我正在使用的代码是

library(ggplot2)
library(ggmap)

MyMap <- get_map(location = c(lon = -96.5, lat = 40.68925), zoom = 4,maptype = "terrain", scale = 2)
ggmap(MyMap)+ 
geom_point(data = data,aes(x = Longitude , y = Latitude ),size=sqrt(data$amount)/800,col='darkred', shape = 19,alpha = .5) 

现在,我想在此情节中添加图例.图例应在地图上显示对应于一定数量的圆圈的大小.我该怎么做?

Now I want to add legend to this plot. The legend should show the sizes of the circles on the map correspond to certain amount. How can I do it?

推荐答案

size参数应包含在geom_point函数的aes()部分中,如下所示:

The size argument should be included within the aes() section of the geom_point function, as follows:

plot <- ggmap(MyMap) + 
  geom_point(data = data,aes(x = Longitude , y = Latitude, size=amount), col='darkred', shape = 19,alpha = .5)
plot 

如果要进一步定制秤,可以使用可选参数scale_size_area()选择

If you want to have further customisation of the scale, you can use the optional argument scale_size_area() to choose the breaks and labels for the legend. For example:

plot +  scale_size_area(breaks = c(436774, 1115087, 4000000, 5068957),
          labels = c("436774", "1115087", "4000000", "5068957"))

更改点大小:

如果要调整点的大小,最好使用scale_size功能,该功能可以指定范围:

If you want to adjust the size of the points, you are better off using the scale_size function, which lets you specify a range:

plot +  scale_size(range = c(5,9))

这篇关于将图例添加到ggmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆