# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
install.packages("jsonlite")
}
if (!requireNamespace("tidyverse", quietly = TRUE)) {
install.packages("tidyverse")
}
if (!requireNamespace("ggthemes", quietly = TRUE)) {
install.packages("ggthemes")
}
# Load packages
library(data.table)
library(jsonlite)
library(tidyverse)
library(ggthemes)Gantt
Hiplot website
This page is the tutorial for source code version of the Hiplot Gantt plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:
The Gantt chart is a type of bar chart that illustrates a project schedule.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
data.table;jsonlite;tidyverse;ggthemes
sessioninfo::session_info("attached")β Session info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
setting value
version R version 4.5.2 (2025-10-31)
os Ubuntu 24.04.3 LTS
system x86_64, linux-gnu
ui X11
language (EN)
collate C.UTF-8
ctype C.UTF-8
tz UTC
date 2026-01-17
pandoc 3.1.3 @ /usr/bin/ (via rmarkdown)
quarto 1.8.27 @ /usr/local/bin/quarto
β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
package * version date (UTC) lib source
data.table * 1.18.0 2025-12-24 [1] RSPM
dplyr * 1.1.4 2023-11-17 [1] RSPM
forcats * 1.0.1 2025-09-25 [1] RSPM
ggplot2 * 4.0.1 2025-11-14 [1] RSPM
ggthemes * 5.2.0 2025-11-30 [1] RSPM
jsonlite * 2.0.0 2025-03-27 [1] RSPM
lubridate * 1.9.4 2024-12-08 [1] RSPM
purrr * 1.2.1 2026-01-09 [1] RSPM
readr * 2.1.6 2025-11-14 [1] RSPM
stringr * 1.6.0 2025-11-04 [1] RSPM
tibble * 3.3.1 2026-01-11 [1] RSPM
tidyr * 1.3.2 2025-12-19 [1] RSPM
tidyverse * 2.0.0 2023-02-22 [1] RSPM
[1] /home/runner/work/_temp/Library
[2] /opt/R/4.5.2/lib/R/site-library
[3] /opt/R/4.5.2/lib/R/library
* ββ Packages attached to the search path.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Data Preparation
Data were loaded for 4 samples (4 patients), 3 items (3 treatments), and the start and end times of each treatment.
# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/gantt/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
# Convert data structure
usr_ylab <- colnames(data)[1]
if (!is.numeric(data[, 2])) {
data[, 2] <- factor(data[, 2], levels = unique(data[, 2]))
}
data_gather <- gather(data, "state", "date", 3:4)
sample <- levels(data_gather$sample)
data_gather$sample <- factor(data_gather$sample,
levels = rev(unique(data_gather$sample))
)
# View data
head(data_gather) sample item state date
1 patient1 treat1 start 1
2 patient2 treat1 start 2
3 patient2 treat2 start 5
4 patient2 treat3 start 7
5 patient3 treat3 start 6
6 patient3 treat1 start 3
Visualization
# Gantt
p <- ggplot(data_gather, aes(date, sample, color = item)) +
geom_line(size = 10, alpha = 1) +
labs(x = "Time", y = "sample", title = "Gantt Plot") +
theme(axis.ticks = element_blank()) +
scale_color_manual(values = c("#e04d39","#5bbad6","#1e9f86")) +
theme_stata() +
theme(text = element_text(family = "Arial"),
plot.title = element_text(size = 12,hjust = 0.5),
axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 1),
legend.position = "right",
legend.direction = "vertical",
legend.title = element_text(size = 10),
legend.text = element_text(size = 10))
p
The horizontal axis represents the time schedule, the vertical axis represents 4 patients, and the 3 colors represent 3 treatments. The figure can observe the time schedule of different treatments for each patient.
