使用 CalendR 绘制船只的捕鱼日。您如何重新排序图例级别例如首先是船名,最后是公共假期?
加载包、创建事件和绘图日历:
## load packages
library(tidyverse)
library(calendR)
library(viridis)
## events
events <- rep(NA, 366) ## 2024 is a leap year
## corresponding events
events[c(3, 22, 45, 63, 100, 140, 195, 234, 300)] <- "Big Catch" ## boat 1
events[c(36, 59, 78, 123, 167, 210, 289, 340)] <- "Fish Tales" ## boat 2
events[c(25, 80, 90, 155, 255, 297, 339)] <- "Marlin" ## boat 3
events[c(1, 89, 92, 142, 143, 239, 360, 361)] <- "Holiday" ## holidays
## holiday to come last
desired_order <- factor(c(levels = c("Big Catch", "Fish Tales", "Marlin", "Holiday")))
## select desired order and colourway
show_col(viridis_pal()(5)) ## colour palette = viridis
## order colours
ordered_colours <- c("#3b528bff", "#21908cff", "#5dc863ff", "lightgray")[order(desired_order)]
## plot
calendR(year = 2024, start = "M",
special.days = events,
special.col = c(ordered_colours),
lty = 1, bg.col = "white",
subtitle.col = 1,
weeknames = c("M", "T", "W", "T", "F", "S", "S"),
day.size = 3.5,
orientation = "l",
legend.pos = "bottom")
我正在寻找显示 Big Catch 的图例,然后是 Fish Tales,然后是 Marlin,最后是 Holiday。我的在绘图时切换到字母顺序。
尚未找到设置
fill
颜色顺序的选项。但一种选择是使用 scale_fill_manual
并通过 limits=
参数设置顺序:
library(calendR)
library(ggplot2)
ordered_colours <- setNames(ordered_colours, c("Big Catch", "Fish Tales", "Marlin", "Holiday"))
calendR(
year = 2024, start = "M",
special.days = events,
special.col = c(ordered_colours),
lty = 1, bg.col = "white",
subtitle.col = 1,
weeknames = c("M", "T", "W", "T", "F", "S", "S"),
day.size = 3.5,
orientation = "l",
legend.pos = "bottom"
) +
scale_fill_manual(
values = ordered_colours,
limits = names(ordered_colours),
na.value = "transparent"
)
#> Scale for fill is already present.
#> Adding another scale for fill, which will replace the existing scale.