如何为ggplot的scale_colour_binned添加更多颜色调色板?
问题
用ggplot2和ggOceanMaps绘制地图时,通过scale_colour_binned()实现日期的颜色分箱效果,尝试将type参数改为"magma"等调色板时,报错提示仅支持"viridis"或"gradient",如何为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"))

解决方法
1. 使用scale_colour_stepsn()自定义分箱调色板
这是最灵活的方案,支持所有调色板(包括viridis系列、RColorBrewer、自定义颜色),直接指定颜色向量即可实现分箱渐变。
需要先加载viridis包获取magma调色板:
library(ggOceanMaps) library(tidyverse) library(lubridate) library(viridis) 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) + ggspatial::geom_spatial_point(data = df_map, aes(x = Lon, y = Lat, color = date_numeric), size = 2) + scale_colour_stepsn( name = "Date", # 从magma调色板提取对应数量的颜色 colours = magma(n = length(unique(df_map$date_numeric))), 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") )
2. 结合scale_colour_binned()与gradient类型
如果坚持使用scale_colour_binned(),可以将type设为"gradient",通过low/high指定渐变两端,或用colors传入完整颜色向量:
方式A:指定渐变首尾颜色
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_binned( type = "gradient", name = "Date", # 从magma调色板取首尾颜色 low = magma(10)[1], high = magma(10)[10], 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") )
方式B:使用调色板函数生成颜色
借助viridis_pal()生成指定类型的调色板函数,配合scale_colour_binned()的palette参数:
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_binned( type = "gradient", name = "Date", palette = viridis_pal(option = "magma"), 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") )
示例数据
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")
内容的提问来源于stack exchange,提问作者C. Guff
相关产品推荐
相关产品推荐

