使用来自不同数据集的 geom_point 个位置向 ggmap 添加图例
adding legend to ggmap with geom_point positions from different datasets
我一直在用来自不同数据集 (MR + MRTag) 的 geom_point 绘制两组位置数据。
MR
detect_date Latitude Longitude species
12/04/2016 11:08 -6.6524 71.3475 Manta Ray
12/04/2016 11:09 -6.6524 71.3475 Manta Ray
12/04/2016 11:10 -6.6524 71.3475 Manta Ray
16/04/2016 21:27 -6.6524 71.3475 Manta Ray
MRTag
species taggingdate deploy_lat deploy_lon
Manta Ray 3/24/2016 -5.4191 71.83855
Manta Ray 02/05/2013 -5.2568 71.65768333
Manta Ray 02/07/2013 -5.33448 71.9812
Manta Ray 02/08/2013 -5.3046 71.94231667
然后我用下面的代码做了一个底图
library(ggmap)
MR_bbox <- make_bbox(lon = MR$Longitude, lat = MR$Latitude, f=1)
MR_map <- get_map(location = MR_bbox, source = "google", maptype = "satellite")
然后将我的位置数据从 Mr + MRTag 绘制到地图上
MR_plot <-
ggmap(MR_map) +
geom_point(data = MR, mapping = aes(x = Longitude, y = Latitude),
shape = 1, color = "white") +
geom_point(data = MRTag, mapping = aes(x = deploy_lon, y = deploy_lat),
shape = 25, fill = "#00FF00", color = "#00FF00") +
xlab("Longitude") +
ylab("Latitude")
这给了我这个情节
我想在地图上为这两个形状添加图例,但我不确定最好的方法,目前的方法还没有奏效
解决方法实际上非常简单:只需将相关的美学论据移至 aes()
。
MR_plot <- ggmap(MR_map) +
geom_point(data = MR, aes(x = Longitude, y = Latitude,
color = 'detect', shape = 'detect', fill = 'detect')) +
geom_point(data = MRTag, aes(x = deploy_lon, y = deploy_lat,
color = 'tag', shape = 'tag', fill = 'tag')) +
xlab("Longitude") +
ylab("Latitude") +
scale_color_manual(name = 'legend', values = c(detect = 'white', tag = '#00FF00')) +
scale_fill_manual(name = 'legend', values = c(detect = NA, tag = '#00FF00')) +
scale_shape_manual(name = 'legend', values = c(detect = 1, tag = 25))
实际上有点令人困惑:如果您在 aes()
之外使用 color=
,那么您设置了这些点的实际颜色并且没有图例,因为这纯粹是一种美学选择。
如果您在 aes()
中使用 color=
(或 fill=
或 shape=
),但是,您正在做的是定义另一个控制颜色的变量的点。例如,如果您想根据物种为这些点着色,您的 aes()
将是:aes(x = Longitude, y = Latitude, color = species)
。这是同一件事:除了通过传递单个字符串,您将 geom_point
中所有点的颜色变量设置为单个值。
然后您必须使用 scale_color_*
函数(以及相应的填充和形状函数)将这些颜色变量设置为您想要的特定颜色。默认情况下,aes()
中定义的每个比例尺都会有自己的图例。但是如果你给传说命名,给它们起一个相同的名字,那么它们就会合二为一。
我一直在用来自不同数据集 (MR + MRTag) 的 geom_point 绘制两组位置数据。
MR
detect_date Latitude Longitude species
12/04/2016 11:08 -6.6524 71.3475 Manta Ray
12/04/2016 11:09 -6.6524 71.3475 Manta Ray
12/04/2016 11:10 -6.6524 71.3475 Manta Ray
16/04/2016 21:27 -6.6524 71.3475 Manta Ray
MRTag
species taggingdate deploy_lat deploy_lon
Manta Ray 3/24/2016 -5.4191 71.83855
Manta Ray 02/05/2013 -5.2568 71.65768333
Manta Ray 02/07/2013 -5.33448 71.9812
Manta Ray 02/08/2013 -5.3046 71.94231667
然后我用下面的代码做了一个底图
library(ggmap)
MR_bbox <- make_bbox(lon = MR$Longitude, lat = MR$Latitude, f=1)
MR_map <- get_map(location = MR_bbox, source = "google", maptype = "satellite")
然后将我的位置数据从 Mr + MRTag 绘制到地图上
MR_plot <-
ggmap(MR_map) +
geom_point(data = MR, mapping = aes(x = Longitude, y = Latitude),
shape = 1, color = "white") +
geom_point(data = MRTag, mapping = aes(x = deploy_lon, y = deploy_lat),
shape = 25, fill = "#00FF00", color = "#00FF00") +
xlab("Longitude") +
ylab("Latitude")
这给了我这个情节
我想在地图上为这两个形状添加图例,但我不确定最好的方法,目前的方法还没有奏效
解决方法实际上非常简单:只需将相关的美学论据移至 aes()
。
MR_plot <- ggmap(MR_map) +
geom_point(data = MR, aes(x = Longitude, y = Latitude,
color = 'detect', shape = 'detect', fill = 'detect')) +
geom_point(data = MRTag, aes(x = deploy_lon, y = deploy_lat,
color = 'tag', shape = 'tag', fill = 'tag')) +
xlab("Longitude") +
ylab("Latitude") +
scale_color_manual(name = 'legend', values = c(detect = 'white', tag = '#00FF00')) +
scale_fill_manual(name = 'legend', values = c(detect = NA, tag = '#00FF00')) +
scale_shape_manual(name = 'legend', values = c(detect = 1, tag = 25))
实际上有点令人困惑:如果您在 aes()
之外使用 color=
,那么您设置了这些点的实际颜色并且没有图例,因为这纯粹是一种美学选择。
如果您在 aes()
中使用 color=
(或 fill=
或 shape=
),但是,您正在做的是定义另一个控制颜色的变量的点。例如,如果您想根据物种为这些点着色,您的 aes()
将是:aes(x = Longitude, y = Latitude, color = species)
。这是同一件事:除了通过传递单个字符串,您将 geom_point
中所有点的颜色变量设置为单个值。
然后您必须使用 scale_color_*
函数(以及相应的填充和形状函数)将这些颜色变量设置为您想要的特定颜色。默认情况下,aes()
中定义的每个比例尺都会有自己的图例。但是如果你给传说命名,给它们起一个相同的名字,那么它们就会合二为一。