繁体   English   中英

如何在不更改 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 会抱怨“连续变量无法映射到形状”。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM