You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggOceanMaps与ggplot2中移除图例黑色小方框

移除ggOceanMaps图例中的黑色方框

我想要移除下图图例中的黑色方框:

当前绘图效果

期望达到的效果如下:

期望绘图效果


示例数据

df_map <- structure(list(station = c("D20230722", "D20230723", "D20230724", 
"D20230725", "D20230726", "D20230727"), Lat = c(78.4464862016807, 
79.019481225, 79.2393951333333, 79.1554491333333, 79.34182725, 
79.3757057), Lon = c(25.5186327815126, 25.131371675, 28.0058926, 
33.630775, 33.8795404833333, 33.7051135166667)), row.names = c(NA, 
6L), class = "data.frame")

原绘图代码

library(ggOceanMaps)
library(tidyverse)

path = "Path/to/output/"

pdf(paste0(path,"Localisation_temp.pdf"), width=12, height=8)
basemap(data = df_map, limits = c(min(df_map$Lon) - 0.5,max(df_map$Lon) + 0.5,min(df_map$Lat) - 0.5,max(df_map$Lat) + 0.5), bathymetry = TRUE,
        # bathy.style = "rcb" proper raster
        bathy.style = "rbg"
) + # a synonym: basemap(dt)
  ggspatial::geom_spatial_point(data = df_map, aes(x = Lon, y = Lat, color = station), fill = "white", size = 3) + theme(legend.key = element_rect(fill = "white")) #remove rectangles legends
dev.off()

已尝试的方法(无效)

我尝试通过guide = guide_legend(override.aes = list(fill_new = NA))(或list(color_new = NA))修改图例样式,修改后的代码如下,但输出结果没有变化,黑色方框仍然存在:

library(ggOceanMaps)
library(tidyverse)
library(ggnewscale)
library(pals)

basemap(data = df_map, limits = c(min(df_map$Lon) - 0.5,max(df_map$Lon) + 0.5,min(df_map$Lat) - 0.5,max(df_map$Lat) + 0.5), bathymetry = TRUE,
        # bathy.style = "rcb" proper raster
        bathy.style = "rbg"
) + # a synonym: basemap(dt)
  ggspatial::geom_spatial_point(data = df_map, aes(x = Lon, y = Lat, color = as.factor(station)), fill = "white", size = 3) + 
  scale_color_manual(
    values = as.vector(alphabet(18)),
    guide = guide_legend(
      override.aes = list(color_new = NA)
    )
  )

解决方案

问题出在图例标记的边框设置以及ggspatial::geom_spatial_point的默认形状属性上,可通过以下两种方式解决:

方法一:覆盖图例的边框样式

在scale_color_manual的override.aes中直接设置color = NA,同时将图例背景设为透明:

library(ggOceanMaps)
library(tidyverse)
library(pals)

basemap(data = df_map, limits = c(min(df_map$Lon)-0.5, max(df_map$Lon)+0.5, min(df_map$Lat)-0.5, max(df_map$Lat)+0.5), 
        bathymetry = TRUE, bathy.style = "rbg") +
  ggspatial::geom_spatial_point(data = df_map, aes(x = Lon, y = Lat, color = station), fill = "white", size = 3) +
  scale_color_manual(values = as.vector(alphabet(18)),
                     guide = guide_legend(override.aes = list(color = NA))) +
  theme(legend.key = element_rect(fill = NA)) # 移除图例背景框

方法二:修改点的形状为实心样式

如果不需要白色填充的点,直接将shape设为实心样式(如shape = 16),图例就不会出现黑色边框:

library(ggOceanMaps)
library(tidyverse)

path = "Path/to/output/"

pdf(paste0(path,"Localisation_temp.pdf"), width=12, height=8)
basemap(data = df_map, limits = c(min(df_map$Lon)-0.5, max(df_map$Lon)+0.5, min(df_map$Lat)-0.5, max(df_map$Lat)+0.5), 
        bathymetry = TRUE, bathy.style = "rbg") +
  ggspatial::geom_spatial_point(data = df_map, aes(x = Lon, y = Lat, color = station), size = 3, shape = 16) +
  theme(legend.key = element_rect(fill = NA))
dev.off()

内容的提问来源于stack exchange,提问作者C. Guff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 06:31:02