MikkoVihtakari / ggOceanMaps

Plot oceanographic research data on maps using ggplot2
GNU General Public License v3.0
46 stars 7 forks source link

Dark legend key when combining basemap with geom_spatial_point #42

Closed cpagniel closed 4 months ago

cpagniel commented 4 months ago

I am trying to combine basemap from ggOceanMaps with geom_spatial_point and having the geom_spatial_point having a new color scale.

When I combine these two, the legend.key background color remains dark grey despite setting it to white in the theme. If I remove the basemap, there is no issues.

Any thoughts?

Here's the code (and image of plot):

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

basemap(limits = c(110, -110, 20, 60), rotate = TRUE,bathy.style = "rcb") +
  new_scale_fill() +
  geom_spatial_point(crs = 4326, data = sub, aes(x = lon, y = lat, fill = factor(toppid)), size = 2, pch = 21, color = "black") +
  xlab("") + ylab("") +
  scale_fill_manual(values = as.vector(alphabet(18)), guide = guide_legend(ncol = 3, title = "toppID")) +
  annotation_scale(location = "bl",text_cex = 1) +
  annotation_north_arrow(location = "tr", which_north = "true") +
  theme_bw() + 
  theme(axis.text.y   = element_text(size=14),
        axis.text.x   = element_text(size=14),
        plot.title = element_text(size=18, face="bold"),
        legend.title = element_text(size=14, face="bold"),
        legend.text = element_text(size=12),
        legend.position = "bottom", 
        panel.background = element_rect(fill = "white", color = NA),
        axis.line = element_line(color = "black"),
        panel.border = element_rect(color = "black", fill=NA, size=2),
        legend.key = element_rect(fill = "white", color = NA))
cpagniel commented 4 months ago

A simpler example that reproduces the same error as above (i.e., not white legend.key background):

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))
cpagniel commented 4 months ago

Found an answer!

MikkoVihtakari commented 3 months ago

Nice that you found a solution and sorry for delayed reply. I was on a holiday.