Line Graph

Visualising simulated dataset for a clinical trial, usingĀ a Line Graph.

R code

# Libraries -------------------------------------------------------------

library(tidyverse)
library(RColorBrewer)
# Data -------------------------------------------------------------

years <- c(2015:2018)
months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

clinical.trial <- tibble(year = factor(rep(years , each = length(months)), levels = years),
age = sample(100, length(years)*length(months) ),
months = factor(rep(months, 4), levels = months)
)
clinical.trial <- clinical.trial %>% arrange(desc(year))
# Theme -------------------------------------------------------------

bg_color <- "#fafafa"

theme_clinical <- function(){

theme(text = element_text(family = "PT Serif"),
plot.background = element_rect(fill = bg_color, color = bg_color),
plot.margin = unit( rep(40, 4) , "points"), # add plot margin
plot.title = element_text(),
plot.subtitle = element_text(margin = unit(c(5, 0, 0, 0) , "points"), color = "#666666", face = "italic"),
plot.caption = element_text(margin = unit(c(40, 0, 0, 0) , "points"), color = "#666666", face = "italic"),
strip.text = element_text(margin = unit(c(20, 0, 20, 0) , "points")),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_line(size = 0.3),
axis.text.x = element_text(margin = unit(c(20, 0, 0, 0) , "points")),
legend.position = "none"
)
}
# Plot -------------------------------------------------------------

plot <- ggplot(clinical.trial, aes(x = months, y = age )) +
geom_line(group = 1, lineend = "round", color = "blue", alpha = 1/5) +
geom_point(size = 2.5, stroke = 1, shape = 21, fill = "blue", color = bg_color) +
labs(x = NULL,
y = NULL,
title = "Hypothetical Title",
subtitle = paste("hypothetical subtitle for the period", years[1], " - ", years[length(years)]),
#subtitle = "and a hypothetical subtitle",
caption = "Simulated data from a hypothetical clinical trial") + # hide x and y axis labels
scale_y_discrete(limits = c(0, 100), expand = c(0.1, 0.1) ) +
theme_minimal() +
theme_clinical() +
facet_wrap(~ year, ncol = 1, scales = "fixed")

plot
# Export -------------------------------------------------------------

#ggsave(plot, width = 8, height = 12, device = cairo_pdf, filename = "clinical.trial.year.pdf")

Keyshape prototype