Requires ‘Lato’ family of fonts, and the extrafont R package.
# Import and wrangle - plus load fonts
data <- read.csv(file.choose(), header = T)
library(tidyverse)
library(extrafont)
font_import(pattern = 'Lato')
## Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
## Continue? [y/n]
data$TST_hrs_day <- as.integer(data$TST_hrs_day)
data$Species <- as.character(data$Species)
loadfonts(device = 'win')
# Plot Code
cols <- c('#044c66', '#044c66', '#044c66', '#044c66', '#044c66', '#FF9933', '#FF9933',
'#FF9933', '#044c66', '#044c66', '#044c66', '#044c66',
'#044c66', '#044c66', '#FF9933', '#044c66', '#FF9933', '#044c66', '#044c66')
cols_2 <- c('#CC6600', '#CC6600', '', '', '#CC6600', '#CC6600', '#CC6600',
'#CC6600', '#CC6600', '#CC6600', '', '',
'#CC6600', '', '#CC6600', '#CC6600', '#CC6600', '#CC6600', '#CC6600')
ggplot(data = data, aes(x = reorder(data$Species, +data$TST_percent_24hr),
y = data$TST_percent_24hr, colour = data$TST_percent_24hr)) +
geom_bar(stat='identity', width=0.8, colour = 'steel blue', alpha = .7, fill = cols) +
labs(x="", y="", title="Average time spent asleep each day varies drastically from species to species.
For humans, changes in how long we spend asleep due to ageing are
also significant",
caption= "Data sources: Ohayon et al. (2004), Kryger et al. (2015), Parmelee et al. (1964)") +
scale_y_continuous(labels = c("0%", "20%", "40%", "60%", "80%"))+
geom_hline(yintercept= c(20, 40, 60, 80), size=0.3, linetype = 4)+
coord_flip() +
theme_classic() +
theme(legend.position='none', text = element_text(family = 'Lato Light'),
axis.text.x = element_text(face = "plain", size = 9),
axis.text.y = element_text(colour = cols, size = 10, family = "Lato Medium"),
axis.line = element_line(colour = '#003366', size = 1),
title = element_text(size = 12, colour = 'black', family = 'Lato Medium'),
plot.caption = element_text(size = 8, colour = 'black', family = 'Lato Hairline'))+
geom_text(aes(label = paste0(round(data$TST_percent_24hr*0.24, digits = 1),
as.character("hrs")), family = 'Lato',
hjust = 1.0, colour = cols_2))