I am plotting a map using ggplot
and ggOceanMaps
packages. To have binned colours in function of the date, I used the function scale_colour_binned()
.
library(ggOceanMaps)
library(tidyverse)
library(lubridate)
df_map <- df_map %>%
mutate(date_numeric = as.numeric(date))
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
) + # a synonym: basemap(dt)
ggspatial::geom_spatial_point(data = df_map, aes(x = Lon, y = Lat, color = date_numeric), size = 2) +
# scale_colour_distiller(labels = \(x) as.POSIXct(x, origin = lubridate::origin) |>
# format(format ="%b %d"), n.breaks = length(unique(df_map$station))/2, palette = "Spectral")
scale_colour_binned(type = "magma", name="Date",
breaks = seq(min(df_map$date_numeric), max(df_map$date_numeric), length.out = length(unique(df_map$date_numeric))),
labels = \(x) as.POSIXct(x, origin = lubridate::origin) |>
format(format = "%b %d"))
Unfortunately, when I try to change the type = "viridis"
for a different colour palette (e.g. magma), I receive an error telling me the scale type "magma" is unknown, and only "vidris" or "gradient" are available. Is there a way to have more palettes available for the scale_colour_binned()
function?
Here is an example of data :
df_map = structure(list(Lat = c(78.6585508333333, 78.6585508333333, 78.6585508333333,
78.6585508333333, 78.6585508333333, 78.6585508333333, 78.6585508333333,
78.6585508333333, 78.7585508333333, 78.7585508333333, 78.7585508333333,
79.6585508333333, 79.6585508333333, 79.6585508333333, 79.6585508333333,
79.7585508333333, 79.7585508333333, 79.7585508333333, 80.6585508333333,
80.6585508333333, 75.0000000000000, 75.1000000000000, 73.9994955166667, 73.9994955166667, 73.9994955166667,
73.9994955166667, 73.9994955166667), Lon = c(24.6500727416667, 24.6500727416667,
24.6500727416667, 24.6500727416667, 24.6500727416667, 24.6500727416667,
24.6500727416667, 24.6500727416667, 24.6500727416667, 24.6500727416667,
24.6500727416667, 24.6500727416667, 24.6500727416667, 24.6500727416667,
23.6500727416667, 23.6500727416667, 23.6500727416667, 25.7500727416667,
25.7500727416667, 25.7500727416667, 30.9864643333333, 30.8874643333333, 35.9974643333333,
35.9974643333333, 35.9974643333333, 35.9974643333333, 35.9974643333333), date = structure(c(1690042620,
1690042620, 1690042620, 1690042620, 1690042620, 1690042620, 1690042620,
1690042620, 1690042620, 1690042620, 1690192620, 1690192620, 1690192620,
1690192620, 1690192620, 1690392620, 1690392620, 1690542620, 1690542620,
1690542620, 1691056620, 1691056620, 1691686620, 1691686620, 1691686620, 1691686620,
1691686620), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA,
27L), class = "data.frame")
scale_colour_binned()
in ggplot2
only accepts two values for its type
argument: "gradient"
and "viridis"
. If you want to use the magma palette (or any of the other viridis family) in a binned color scale, you should use scale_colour_viridis_b()
. This function is designed to work with binned scales using the viridis palette and allows you to specify the palette via the option
argument.
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) +
ggspatial::geom_spatial_point(data = df_map, aes(x = Lon, y = Lat, color = date_numeric), size = 2) +
scale_colour_viridis_b(option = "magma", name = "Date",
breaks = seq(min(df_map$date_numeric),
max(df_map$date_numeric),
length.out = length(unique(df_map$date_numeric))),
labels = function(x) {
as.POSIXct(x, origin = lubridate::origin) |>
format("%b %d")
})