giocomai / ganttrify

Create beautiful Gantt charts with ggplot2
https://ganttrify.europeandatajournalism.eu/
GNU General Public License v3.0
658 stars 62 forks source link

Introduce week-based projects #44

Open giocomai opened 1 year ago

giocomai commented 1 year ago

There is currently not an easy way to show projects based on weeks, rather than months.

A consistent implementation requires some choices that may not be obvious, e.g. should the gray stripes in the background be then based on weeks rather than months? Probably.

Which dates should be shown on the x-axis? Probably the date in which each 7-day period starts.

Until a more consistent implementation will be introduced, here's a hacky workaround.

It requires providing as dates the start and end points of activities (a more consistent way would allow to pass week numbers).

library("ganttrify")
project <- ganttrify::test_project_date_day

# This is just to shorten a bit the example project
project$end_date[project$activity=="Active promo"] <- "2020-11-17"

base_gg <- ganttrify(project = project,
                     by_date = TRUE,
                     # colour_stripe = "lightgray",
                     colour_stripe = NA,
                     exact_date = TRUE) 

# break by weeks 
week_breaks <- seq.Date(from = min(lubridate::as_date(project$start_date)),
                        to = max(lubridate::as_date(project$end_date)),
                        by = 7)

base_gg +
  ggplot2::scale_x_date(name = "",
                        breaks = week_breaks,
                        date_labels = "%e %b\n%Y",
                        minor_breaks = NULL,
                        limits = c(min(lubridate::as_date(project$start_date)),
                                   max(lubridate::as_date(project$end_date))+3),
                        sec.axis = ggplot2::dup_axis(breaks = week_breaks+3.5,
                                                     labels = paste0("W", seq_along(week_breaks))))
#> Scale for x is already present.
#> Adding another scale for x, which will replace the existing scale.
#> Warning: Removed 1 rows containing missing values (`geom_rect()`).

Created on 2023-07-29 with reprex v2.0.2

kridsadakorn commented 5 months ago

@giocomai Thank you so much this is very useful.