Skip to content

Commit

Permalink
rely on ggtext to remove internal suppressWarnings and enable customi…
Browse files Browse the repository at this point in the history
…sations in activity labels
  • Loading branch information
giocomai committed Jan 22, 2024
1 parent 91f83e0 commit 052133f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ganttrify
Title: Create beautiful Gantt charts with ggplot2
Version: 0.0.0.9011
Version: 0.0.0.9012
Authors@R:
person(given = "Giorgio",
family = "Comai (OBCT/CCI)",
Expand All @@ -23,7 +23,8 @@ Imports:
lubridate,
tidyr,
shiny,
DT
DT,
ggtext
URL: https://github.com/giocomai/ganttrify
BugReports: https://github.com/giocomai/ganttrify/issues
Language: en-US
Expand Down
26 changes: 18 additions & 8 deletions R/ganttrify.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#' used to represent WPs.
#' @param hide_wp Logical, defaults to FALSE. If TRUE, the lines of the WP are
#' hidden and only activities are shown.
#' @param wp_label_bold Logical, defaults to \code{TRUE}. If \code{TRUE}, the
#' label for working packages is set to bold face, while activities remain
#' plain. Set to \code{FALSE} to keep have all labels in plain face or for
#' further customisation (`ganttrify` offers basic markdown support).
#' @param size_activity Numeric, defaults to 4. It defines the thickness of the
#' line used to represent activities.
#' @param size_text_relative Numeric, defaults to 1. Changes the size of all
Expand Down Expand Up @@ -109,6 +113,7 @@ ganttrify <- function(project,
mark_years = FALSE,
size_wp = 6,
hide_wp = FALSE,
wp_label_bold = TRUE,
size_activity = 4,
size_text_relative = 1,
label_wrap = FALSE,
Expand Down Expand Up @@ -262,12 +267,20 @@ ganttrify <- function(project,
dplyr::group_by(wp) %>%
dplyr::summarise(wp_activity = list(wp_activity)) %>%
dplyr::group_by(wp) %>%
dplyr::mutate(wp = stringr::str_c(wp, wp, sep = "_"))
dplyr::mutate(wp = stringr::str_c(wp, wp, sep = "_")) %>%
dplyr::ungroup()

distinct_yearmon_labels_df <- df_yearmon %>%
dplyr::distinct(wp, activity) %>%
dplyr::group_by(wp) %>%
dplyr::summarise(activity = list(activity))
dplyr::summarise(activity = list(activity)) %>%
dplyr::ungroup()

if (wp_label_bold) {
distinct_yearmon_labels_df <- distinct_yearmon_labels_df %>%
dplyr::mutate(wp = stringr::str_c("<b>", wp, "</b>"))
}


level_labels_df <- tibble::tibble(
levels = rev(unlist(t(matrix(c(distinct_yearmon_levels_df$wp, distinct_yearmon_levels_df$wp_activity), ncol = 2)))),
Expand Down Expand Up @@ -413,7 +426,7 @@ ganttrify <- function(project,
axis_text_align_n <- 1
}

gg_gantt <- suppressWarnings(gg_gantt +
gg_gantt <- gg_gantt +
ggplot2::scale_y_discrete(
name = NULL,
breaks = level_labels_df$levels,
Expand All @@ -423,16 +436,13 @@ ganttrify <- function(project,
ggplot2::scale_colour_manual(values = colour_palette) +
ggplot2::theme(
text = ggplot2::element_text(family = font_family),
axis.text.y.left = ggplot2::element_text(
face = ifelse(test = df_yearmon_fct %>%
dplyr::distinct(activity, wp, type) %>%
dplyr::pull(type) == "wp", yes = "bold", no = "plain"),
axis.text.y.left = ggtext::element_markdown(
size = ggplot2::rel(size_text_relative),
hjust = axis_text_align_n
),
axis.text.x = ggplot2::element_text(size = ggplot2::rel(size_text_relative)),
legend.position = "none"
))
)

if (is.null(spots) == FALSE) {
if (is.data.frame(spots) == TRUE) {
Expand Down
21 changes: 21 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,27 @@ And since it's a `ggplot2` object, you can export it as you would any other ggpl

If you are using this in an `rmarkdown` document, keep in mind that you can set the size at the chunk level, e.g. with something like `{r fig.width=12, fig.height=8}` in the chunk header.

### Markdown, html, images... it's all there

Now, to be honest, nobody asked for this. And I'm not even sure it's a good idea. But if you are interested in more customisations of how the wp and activity labels appear, anything supported by [`ggtext`](https://wilkelab.org/ggtext/) should work here: markdown, basic html tags, and even inline images.

```{r funky_project, fig.height=4}
funky_project <- tibble::tribble(
~wp, ~activity, ~start_date, ~end_date,
"<span style = 'color:red;'>Red</span> flavour", "Considering <sup>upper</sup> styles", 1, 6,
"<span style = 'color:red;'>Red</span> flavour", "Or **bold**, or *italic*", 3, 6,
"Don't forget <span style = 'font-size:6pt'>the small things</span>", "Contribute to Wikidata <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/320px-Wikidata-logo.svg.png' width=20>", 5, 10,
"Don't forget <span style = 'font-size:6pt'>the small things</span>", "And to OpenStreetMap <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Openstreetmap_logo.svg/256px-Openstreetmap_logo.svg.png' width=20>", 7, 12
)
ganttrify(
project = funky_project,
project_start_date = "2024-01",
font_family = "Roboto Condensed"
) +
ggplot2::ggtitle("Custom activity labels, nothing else to see here")
```


## Shiny app

Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,33 @@ If you are using this in an `rmarkdown` document, keep in mind that you
can set the size at the chunk level, e.g. with something like
`{r fig.width=12, fig.height=8}` in the chunk header.

### Markdown, html, images… it’s all there

Now, to be honest, nobody asked for this. And I’m not even sure it’s a
good idea. But if you are interested in more customisations of how the
wp and activity labels appear, anything supported by
[`ggtext`](https://wilkelab.org/ggtext/) should work here: markdown,
basic html tags, and even inline images.

``` r
funky_project <- tibble::tribble(
~wp, ~activity, ~start_date, ~end_date,
"<span style = 'color:red;'>Red</span> flavour", "Considering <sup>upper</sup> styles", 1, 6,
"<span style = 'color:red;'>Red</span> flavour", "Or **bold**, or *italic*", 3, 6,
"Don't forget <span style = 'font-size:6pt'>the small things</span>", "Contribute to Wikidata <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/320px-Wikidata-logo.svg.png' width=20>", 5, 10,
"Don't forget <span style = 'font-size:6pt'>the small things</span>", "And to OpenStreetMap <img src='https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Openstreetmap_logo.svg/256px-Openstreetmap_logo.svg.png' width=20>", 7, 12
)

ganttrify(
project = funky_project,
project_start_date = "2024-01",
font_family = "Roboto Condensed"
) +
ggplot2::ggtitle("Custom activity labels, nothing else to see here")
```

<img src="man/figures/README-funky_project-1.png" width="100%" />

## Shiny app

If you prefer interactive web interfaces to coding, you can still have a
Expand Down
Binary file added man/figures/README-funky_project-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions man/ganttrify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 052133f

Please sign in to comment.