Skip to content

Instantly share code, notes, and snippets.

@lcolladotor
Last active March 24, 2022 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcolladotor/280a36bf4e28cce6ec8229c3d4afd83a to your computer and use it in GitHub Desktop.
Save lcolladotor/280a36bf4e28cce6ec8229c3d4afd83a to your computer and use it in GitHub Desktop.
Live coding gantt
## Load required libraries
library("googlesheets4")
library("dplyr")
library("ggplot2")
library("plotly")
## For reproducibility
library("sessioninfo")
## Authenticate with Google
googlesheets4::gs4_auth()
## Read in the data from Keri's Google Sheet
data_keri <-
read_sheet(
"https://docs.google.com/spreadsheets/d/1oRmRly0afPnXAxMcibWbq5G5lWI8Oz1n23jUTqLaqMk/edit#gid=0",
sheet = "Sheet1"
)
## Explore the data in RStudio
View(data_keri)
## Obtain the study info
studies <-
data_keri[[1]][!complete.cases(data_keri) & !is.na(data_keri[[1]])]
## Drop the last study that has no dates
studies <- studies[-length(studies)]
## Keep only the dates
clean <- data_keri[complete.cases(data_keri), ]
## Add the stuies
clean$study <- rep(studies, each = 2)
## Simplify some column names
colnames(clean)[1] <- "stage"
colnames(clean) <- gsub(" ", "_", colnames(clean))
## Remove "Data " from the stage
clean$stage <- gsub("Data ", "", clean$stage)
## Explore in RStudio the clean data
View(clean)
## Search a bit on Google:
## https://lmgtfy.app/?q=gantt+chart+ggplot2
## Taken from https://jtr13.github.io/cc19/gantt-charts.html :
## transform to Date objects
clean <-
mutate(clean,
start = as.Date(Target_Start),
end = as.Date(Target_End)
)
## Adapted from https://plotly.com/ggplot2/gantt/ :
## make the gantt chart
p <- ggplot(clean) +
geom_linerange(aes(
y = study,
xmin = Target_Start,
xmax = Target_End,
colour = stage
),
size = I(5)
) +
theme_minimal()
## View as a static plot
p
## View as an interactive plot
## Note how the "size" only affects the static plot
ggplotly(p)
## Export to html with RStudio
## https://www.biostars.org/p/458325/#458340
## Save as a pdf
pdf("gantt.pdf", width = 7, height = 3)
p
dev.off()
## Reproducibility information
print("Reproducibility information:")
Sys.time()
proc.time()
options(width = 120)
session_info()
# ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
# setting value
# version R Under development (unstable) (2022-02-28 r81833)
# os macOS Monterey 12.2.1
# system aarch64, darwin20
# ui RStudio
# language (EN)
# collate en_US.UTF-8
# ctype en_US.UTF-8
# tz America/Mexico_City
# date 2022-03-24
# rstudio 2021.09.2+382 Ghost Orchid (desktop)
# pandoc NA
#
# ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
# package * version date (UTC) lib source
# assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.0)
# brio 1.1.3 2021-11-30 [1] CRAN (R 4.2.0)
# cachem 1.0.6 2021-08-19 [1] CRAN (R 4.2.0)
# callr 3.7.0 2021-04-20 [1] CRAN (R 4.2.0)
# cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.2.0)
# cli 3.2.0 2022-02-14 [1] CRAN (R 4.2.0)
# colorout 1.2-2 2022-03-01 [1] Github (jalvesaq/colorout@79931fd)
# colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.2.0)
# crayon 1.5.0 2022-02-14 [1] CRAN (R 4.2.0)
# data.table 1.14.2 2021-09-27 [1] CRAN (R 4.2.0)
# DBI 1.1.2 2021-12-20 [1] CRAN (R 4.2.0)
# desc 1.4.1 2022-03-06 [1] CRAN (R 4.2.0)
# devtools * 2.4.3 2021-11-30 [1] CRAN (R 4.2.0)
# digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0)
# dplyr * 1.0.8 2022-02-08 [1] CRAN (R 4.2.0)
# ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0)
# fansi 1.0.2 2022-01-14 [1] CRAN (R 4.2.0)
# fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0)
# fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0)
# gargle 1.2.0 2021-07-02 [1] CRAN (R 4.2.0)
# generics 0.1.2 2022-01-31 [1] CRAN (R 4.2.0)
# ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.2.0)
# glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0)
# googledrive 2.0.0 2021-07-08 [1] CRAN (R 4.2.0)
# googlesheets4 * 1.0.0 2021-07-21 [1] CRAN (R 4.2.0)
# gtable 0.3.0 2019-03-25 [1] CRAN (R 4.2.0)
# hms 1.1.1 2021-09-26 [1] CRAN (R 4.2.0)
# htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0)
# htmlwidgets 1.5.4 2021-09-08 [1] CRAN (R 4.2.0)
# httr 1.4.2 2020-07-20 [1] CRAN (R 4.2.0)
# jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.2.0)
# lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.2.0)
# lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.2.0)
# lubridate 1.8.0 2021-10-07 [1] CRAN (R 4.2.0)
# magrittr 2.0.2 2022-01-26 [1] CRAN (R 4.2.0)
# memoise 2.0.1 2021-11-26 [1] CRAN (R 4.2.0)
# munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0)
# pillar 1.7.0 2022-02-01 [1] CRAN (R 4.2.0)
# pkgbuild 1.3.1 2021-12-20 [1] CRAN (R 4.2.0)
# pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0)
# pkgload 1.2.4 2021-11-30 [1] CRAN (R 4.2.0)
# plotly * 4.10.0 2021-10-09 [1] CRAN (R 4.2.0)
# prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.2.0)
# processx 3.5.2 2021-04-30 [1] CRAN (R 4.2.0)
# prompt 1.0.1 2022-03-01 [1] Github (gaborcsardi/prompt@7ef0f2e)
# ps 1.6.0 2021-02-28 [1] CRAN (R 4.2.0)
# purrr 0.3.4 2020-04-17 [1] CRAN (R 4.2.0)
# R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0)
# remotes 2.4.2 2021-11-30 [1] CRAN (R 4.2.0)
# rlang 1.0.2 2022-03-04 [1] CRAN (R 4.2.0)
# rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.2.0)
# rsthemes 0.3.1 2022-03-01 [1] Github (gadenbuie/rsthemes@bbe73ca)
# rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0)
# scales 1.1.1 2020-05-11 [1] CRAN (R 4.2.0)
# sessioninfo * 1.2.2 2021-12-06 [1] CRAN (R 4.2.0)
# suncalc 0.5.0 2019-04-03 [1] CRAN (R 4.2.0)
# testthat * 3.1.2 2022-01-20 [1] CRAN (R 4.2.0)
# tibble 3.1.6 2021-11-07 [1] CRAN (R 4.2.0)
# tidyr 1.2.0 2022-02-01 [1] CRAN (R 4.2.0)
# tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.2.0)
# usethis * 2.1.5 2021-12-09 [1] CRAN (R 4.2.0)
# utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0)
# vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.2.0)
# viridisLite 0.4.0 2021-04-13 [1] CRAN (R 4.2.0)
# withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0)
#
# [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#
# ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
## For styling
## styler::style_file("~/Desktop/gantt.R", transformers = biocthis::bioc_style())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment