我正在使用ggmap在海洋上绘制一些填充点,但是它们重叠,因此我使用了colour=black
来给它们画轮廓。当我使用pch=21
时,图例不会更改,并且点会按照我的意愿以黑色勾勒出轮廓。但是,我也试图在地图上获得3个不同的符号。当我指定不同的符号时,图例中的点全部变为黑色。
这里有一些示例数据,以及我用来获取带有正确图例但符号错误的地图的代码
#library
library(ggmap)
library(mapdata)
library(ggplot2)
#sample
mapoc_temp = data.frame(longitude= c(-64.5, -63.1, -62.4, -62.2, -66.0, -61.9),
latitude= c(42.7, 44.8, 45.8, 45.6, 43.0, 40.2),
Zone = sample(c(1,4,6,7), 6, replace = T),
location = sample(c(1,2,3), 6, replace = T))
#map
canada = map_data("worldHires")
ggplot(data = canada) +
borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
#coordinates of my map
coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
#map the receiver locations
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = Zone),
pch = 21,
size = 15, colour = "black") +
#fill the zones
scale_color_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"))
[当我尝试添加符号时,我得到了正确的符号,但是我的图例不再正确。下面是代码和图像。
ggplot(data = canada) +
borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
#coordinates of my map
coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
#map the receiver locations
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = as.factor(Zone)),
pch = pch = if_else(mapoc_temp$location == 1,25,
if_else(mapoc_temp$location == 3, 23, 21)),
size = 15, colour = "black") +
scale_x_continuous(label = abs) +
scale_y_continuous(label = abs) +
#fill the zones in with viridis
scale_color_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"))
有人知道如何在第二张图像中固定图例,以使点是正确的颜色吗?我不需要符号出现在图例中
[一种可能的解决方案是使用Shape
语句创建ifelse
列,然后将该列传递给aes
并调用scale_shape_identity
:
此处创建列:
mapoc_temp$Shape = ifelse(mapoc_temp$location == 1,25,
ifelse(mapoc_temp$location == 3, 23, 21))
这里在情节中使用它:
ggplot(data = canada) +
borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
#coordinates of my map
coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
#map the receiver locations
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = as.factor(Zone),
shape = Shape),
size = 15, colour = "black") +
scale_x_continuous(label = abs) +
scale_y_continuous(label = abs) +
#fill the zones in with viridis
scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
scale_shape_identity()
它回答了您的问题吗?