结合ggOceanMaps和ggspatial时的图例键颜色

问题描述 投票:0回答:1

我正在尝试将 ggOceanMaps 的底图与 geom_spatial_point 结合起来,并使 geom_spatial_point 具有新的色标。

当我将这两者结合起来时,尽管在主题中将其设置为白色,但 legend.key 的背景颜色仍保持深灰色。如果我删除底图,就没有问题。

有什么想法吗?

这是代码(以及显示问题的示例图像):

library(ggspatial)
library(ggnewscale)
library(pals)
library(ggOceanMaps)

test <- data.frame(lon = c(-150:-120), lat = c(20:50), toppid = c(rep(c(10:15), each = 5),15))

basemap(limits = c(110, -110, 20, 60), rotate = TRUE,bathy.style = "rcb") +
  new_scale_fill() +
  geom_spatial_point(crs = 4326, data = test, aes(x = lon, y = lat, fill = factor(toppid)), size = 2, pch = 21, color = "black") +
  scale_fill_manual(values = as.vector(alphabet(6))) +
  theme(legend.key = element_rect(fill = "white", color = NA))

我希望 legend.key 背景为白色。

r ggplot2 legend ggspatial ggnewscale
1个回答
0
投票

我在回答相关问题时遇到了类似的问题。对我来说,图例键的灰色背景似乎源于底图填充。我发现解决此问题的唯一选择是通过

fill
override.aes=
参数覆盖
guide_legend
aes。但是,当您使用
ggnewscale
时,您必须覆盖
fill_new
(又名重命名的
fill
)aes。

library(ggspatial)
library(ggnewscale)
library(pals)
library(ggOceanMaps)

test <- data.frame(lon = c(-150:-120), lat = c(20:50), toppid = c(rep(c(10:15), each = 5), 15))

basemap(limits = c(110, -110, 20, 60), rotate = TRUE, bathy.style = "rcb") +
  new_scale_fill() +
  geom_spatial_point(crs = 4326, data = test, aes(
    x = lon, y = lat,
    fill = factor(toppid)
  ), size = 2, pch = 21, color = "black") +
  scale_fill_manual(
    values = as.vector(alphabet(6)),
    guide = guide_legend(
      override.aes = list(fill_new = NA)
    )
  )

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.