如何在不更改 R 中的图例颜色的情况下使用多个符号、颜色和填充 ggmap?
How to use several symbols, colour, and fill in ggmap without changing the legend colour in R?
我正在使用 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"))
有谁知道如何修复第二张图片中的图例,使点的颜色正确?我不需要图例中的符号
实际上,您的问题是由此处描述的错误引起的:https://github.com/tidyverse/ggplot2/issues/2322。
要过去,你可以这样做:
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(inherit.aes = FALSE, data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = factor(Zone),
shape = factor(location)),
size = 5) +
scale_x_continuous(label = abs) +
scale_y_continuous(label = abs) +
#fill the zones in with viridis
scale_fill_manual(name = "Zone", values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"), breaks = c("1","4","6","7")) +
scale_shape_manual(values = c("1" = 21, "2" = 22, "3" = 24))+
guides(fill = guide_legend(override.aes=list(shape=21)), shape = FALSE)
如@Edward 所述,您可以通过添加 guides(shape = FALSE)
.
来摆脱 shape
图例
它是否回答了您的问题?
我没有你的数据,所以我将使用好的 ol' mtcars 数据集。诀窍是手动分配形状,否则 ggplot 会抱怨 "A continuous variable can not be mapped to shape".
mtcars$shape <- ifelse(mtcars$cyl==4, 21, ifelse(mtcars$cyl==6, 23, 25))
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(fill = factor(cyl), shape=factor(cyl)), col="black", size=3) +
scale_shape_manual(values=c(21,23,25))
对于您自己的数据,请尝试:
mapoc_temp$shape <- factor(mapoc_temp$location + 20)
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = factor(Zone),
shape= shape),
size = 15, colour = "black") +
scale_shape_manual(values=c(21,22,23)) + # Must be same length as unique location.
guides(shape=FALSE) + # If you don't want an extra shape legend.
guides(fill = guide_legend(override.aes=list(shape=21))) # A necessary fudge
我正在使用 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"))
有谁知道如何修复第二张图片中的图例,使点的颜色正确?我不需要图例中的符号
实际上,您的问题是由此处描述的错误引起的:https://github.com/tidyverse/ggplot2/issues/2322。
要过去,你可以这样做:
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(inherit.aes = FALSE, data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = factor(Zone),
shape = factor(location)),
size = 5) +
scale_x_continuous(label = abs) +
scale_y_continuous(label = abs) +
#fill the zones in with viridis
scale_fill_manual(name = "Zone", values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"), breaks = c("1","4","6","7")) +
scale_shape_manual(values = c("1" = 21, "2" = 22, "3" = 24))+
guides(fill = guide_legend(override.aes=list(shape=21)), shape = FALSE)
如@Edward 所述,您可以通过添加 guides(shape = FALSE)
.
shape
图例
它是否回答了您的问题?
我没有你的数据,所以我将使用好的 ol' mtcars 数据集。诀窍是手动分配形状,否则 ggplot 会抱怨 "A continuous variable can not be mapped to shape".
mtcars$shape <- ifelse(mtcars$cyl==4, 21, ifelse(mtcars$cyl==6, 23, 25))
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(fill = factor(cyl), shape=factor(cyl)), col="black", size=3) +
scale_shape_manual(values=c(21,23,25))
对于您自己的数据,请尝试:
mapoc_temp$shape <- factor(mapoc_temp$location + 20)
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = factor(Zone),
shape= shape),
size = 15, colour = "black") +
scale_shape_manual(values=c(21,22,23)) + # Must be same length as unique location.
guides(shape=FALSE) + # If you don't want an extra shape legend.
guides(fill = guide_legend(override.aes=list(shape=21))) # A necessary fudge