我正在尝试做这个日历ggplot2。
到目前为止,它工作正常,但我想进行一项调整,但我没有找到方法:
这是显示蓝色块重叠的输出:
data <- read_csv("country,num_months,start_month_year,end_month_year,B1,B1_p,year,start_month,end_month
2,1,6,6,3.3571016788482666,0.007681768853217363,2021,5,5
2,1,8,8,2.548985481262207,0.007373321335762739,2021,7,7
2,1,10,10,2.139772415161133,0.03452971577644348,2021,9,9
2,1,12,12,2.165775775909424,0.07796278595924377,2021,11,11
2,1,13,13,1.9506219625473022,0.09215697646141052,2021,12,12
2,1,23,23,2.7839596271514893,0.011407249607145786,2022,10,10
2,1,25,25,2.220555543899536,0.06181173026561737,2022,12,12
2,2,10,11,1.4871771335601807,0.06038494035601616,2021,9,10
2,2,12,13,2.0390524864196777,0.04265233874320984,2021,11,12
2,2,13,14,1.417211651802063,0.09348150342702866,2021,12,1
2,2,22,23,1.874699354171753,0.018470995128154755,2022,9,10
2,2,23,24,1.6757967472076416,0.028078539296984673,2022,10,11
2,3,6,8,1.334544062614441,0.05757715925574303,2021,5,7
2,3,8,10,1.2151446342468262,0.05723109096288681,2021,7,9
2,3,10,12,1.7102551460266113,0.03582938387989998,2021,9,11
2,3,11,13,1.6229373216629028,0.07926513999700546,2021,10,12
2,3,12,14,1.6802176237106323,0.04918825626373291,2021,11,1
2,3,13,15,1.417211651802063,0.09348150342702866,2021,12,2
2,3,22,24,1.3854548931121826,0.033461399376392365,2022,9,11
2,3,23,25,1.8822635412216187,0.01415294036269188,2022,10,12
2,4,8,11,1.1319600343704224,0.06593644618988037,2021,7,10
2,4,10,13,1.754726767539978,0.02880094014108181,2021,9,12
2,4,11,14,1.4533793926239014,0.08008415251970291,2021,10,1
2,4,12,15,1.6802176237106323,0.04918825626373291,2021,11,2
2,4,22,25,1.6274609565734863,0.011329303495585918,2022,9,12
2,5,6,10,1.0146392583847046,0.08948446065187454,2021,5,9
2,5,8,12,1.338212251663208,0.04161843657493591,2021,7,11
2,5,9,13,1.2212527990341187,0.0919371098279953,2021,8,12
2,5,10,14,1.5834165811538696,0.03494146466255188,2021,9,1
2,5,11,15,1.4533793926239014,0.08008415251970291,2021,10,2
2,5,12,16,1.4284441471099854,0.0697392150759697,2021,11,3
2,5,21,25,1.2466250658035278,0.035289958119392395,2022,8,12
2,6,6,11,0.9881601333618164,0.08719704300165176,2021,5,10
2,6,8,13,1.438501238822937,0.032221969217061996,2021,7,12
2,6,9,14,1.16400945186615,0.09187468141317368,2021,8,1
2,6,10,15,1.5834165811538696,0.03494146466255188,2021,9,2
2,6,11,16,1.294316291809082,0.09792502969503403,2021,10,3
2,6,12,17,1.4204859733581543,0.0546354204416275,2021,11,4
2,6,20,25,1.07038414478302,0.0722803920507431,2022,7,12") %>%
mutate(start_dt = case_when(
year == 2021 ~ ymd(paste("2021", start_month, "01", sep = "-")),
year == 2022 ~ ymd(paste("2022", start_month, "01", sep = "-"))
),
end_dt = case_when(
year == 2021 ~ ceiling_date(ymd(paste("2021", end_month, "01", sep = "-")), unit = "month") - 1,
year == 2022 ~ ceiling_date(ymd(paste("2022", end_month, "01", sep = "-")), unit = "month") - 1,
),
end_month = ifelse(start_month == end_month, end_month + 1, end_month),
end_month = ifelse(end_month > 12, 1, end_month), # Wrap around to January if end_month exceeds 12
start_dt1 = pmin(start_dt, end_dt),
end_dt1 = pmax(start_dt, end_dt)) %>%
arrange(start_dt1) %>%
mutate(
yy = row_number(),
.by = c(year, num_months)
)
# Create the ggplot
ggplot(data) +
geom_segment(aes(
x = start_dt1, xend = end_dt1,
y = yy, yend = yy,
color = factor(num_months)
), size = 1) +
scale_y_continuous(labels = NULL) +
facet_grid(
rows = vars(num_months), cols = vars(year),
space = "free_y", scales = "free"
) +
labs(x = "Month", y = "Number of Months", color = "Number of Months") +
theme_bw() +
theme(
panel.spacing = unit(1, "lines"), # Increase spacing between panels
panel.grid.major.y = element_blank(),
axis.ticks.y = element_blank()
) +
ggh4x::facetted_pos_scales(
x = lapply(
unique(data$year),
\(x) scale_x_date(
date_labels = "%b",
date_breaks = "1 month",
expand = expansion(0, 0),
limits = as.Date(paste0(x, c("-01-01", "-12-31")))
)
)
)
我在你的
mutate(start_dt = case_when(...
之前添加了这个
mutate(across_yr = end_month < start_month) |>
tidyr::uncount(weights = 1 + across_yr, .id = "copy") |>
mutate(year = year - 1 + copy) |>
mutate(end_month = if_else(copy == 1 & across_yr, 12, end_month),
start_month = if_else(copy == 2, 1, start_month)) |>
要记下跨越一年的行,请将它们复制成两行,然后调整它们,使第一行在 12 月结束,第二行从 1 月开始。