Responsive Line Graph

Visualising life expectancy, using responsive layout Line Graph.

R Code

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

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

datasource <- "http://ismayc.github.io/le_mess.csv"
data <- "data/le_mess.csv"

life_score <- read_csv(data)

country_to_check <- "Bulgaria"
year_to_check <- 1980

country_all <- gather(data = life_score, key = year, value = life_span, - country)
country_selected <- country_all %>% filter(country == country_to_check)

minLife <- min(country_all$life_span)
maxLife <- max(country_all$life_span)

minYear <- parse_number(min(country_selected$year))
maxYear <- parse_number(max(country_selected$year))

minYearLifeSpan <- country_selected %>% filter(country_selected$year == minYear) %>% select(life_span) %>% unlist(use.names = FALSE)
maxYearLifeSpan <- country_selected %>% filter(country_selected$year == maxYear) %>% select(life_span) %>% unlist(use.names = FALSE)

highlight <- country_selected %>% filter(country_selected$year == year_to_check)
# Theme -------------------------------------------------------------------

color_Background <- "#FFFEFB"
color_Primary <- "#282828"
color_Secondary <- "#787878"
color_Accent <- "#CF443C"

size_Base <- 8
size_S <- size_Base / 2
size_M <- size_Base
size_L <- size_Base * 1.5
size_XL <- size_Base * 1.75
size_XXL <- size_Base * 2
size_XXXL <- size_Base * 3

font_family_Primary <- "Open Sans"
font_family_Secondary <- "Open Sans Semibold"
font_size_Title <- size_XL
font_size_Subtitle <- size_L
font_size_Caption <- size_M
font_size_Axis <- size_M

line_Axis <- "solid"
update_geom_defaults("label", list(family = font_family_Secondary, size = 3))

theme_lineplot <- function(){
theme(
text = element_text(family = font_family_Primary),
plot.title = element_text(size = font_size_Title, color = color_Primary, face = "plain", hjust = 0, margin = unit(c(size_XXL, 0, size_M, 0), "pt")),
plot.subtitle = element_text(size = font_size_Subtitle, color = color_Secondary, face = "italic", hjust = 0, margin = unit(c(0, 0, size_XL, 0), "pt") ),
plot.caption = element_text(size = font_size_Caption, color = color_Secondary, face = "italic", hjust = 1, margin = unit(c(size_XXXL, 0, 0, 0), "pt") ),
plot.background = element_rect(fill = color_Background, color = color_Background),
plot.margin = unit( c(size_XXXL, 0, size_XXXL, 0), "pt"),

panel.background = element_rect(fill = NA, colour = NA),
panel.border = element_blank(),
panel.grid = element_blank(),
panel.grid.major = element_line(linetype = line_Axis),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.spacing = unit(size_M, "pt"),

strip.background = element_blank(),
strip.text = element_blank(),

axis.line = element_blank(),
axis.text = element_text(size = font_size_Axis, color = color_Primary),
axis.text.x = element_text(hjust = 0.5, vjust = 0 ),
axis.text.y = element_text(color = color_Accent),
axis.ticks = element_line(color = color_Secondary, size = 0.25),
axis.ticks.y = element_blank(),
axis.ticks.length = unit(size_S, "pt"),
axis.title = element_blank()
)
}
# Plot -------------------------------------------------------------------

plot_Title <- paste("Life expectancy in", country_to_check, "for year", year_to_check)
plot_Subtitle <- "Compared to all countries"
plot_Caption <- paste("Source:", datasource)


lineplot <- ggplot(country_all, aes(x = parse_number(year), y = life_span)) +

  geom_line(aes(group = country), size = 0.25, color = color_Secondary, alpha = 0.1, lineend = "round" ) +
  geom_line(data = country_selected, color = color_Accent, alpha = 1, size = 0.5, lineend = "round" ) +

  annotate(geom = "point", x = year_to_check, y = highlight$life_span, size = 2, color =  color_Accent) +
  annotate(geom = "label", x = year_to_check, y = highlight$life_span, vjust = -0.5, label = paste( round(highlight$life_span), "years"), label.r = unit(1, "pt"), label.padding = unit(6, "pt"), color = color_Background, fill = color_Accent ) +

  scale_y_continuous(breaks = minYearLifeSpan , limits=c(minLife, maxLife), labels = paste(round(minYearLifeSpan), "years"), sec.axis = sec_axis(~., breaks = maxYearLifeSpan, labels = paste(round(maxYearLifeSpan), "years") ) )  +
  scale_x_continuous(breaks=c(minYear, year_to_check, maxYear), expand = c(0.01, 0)) +
  
  coord_cartesian(clip = "off") + 

  labs(title = plot_Title, subtitle = plot_Subtitle, caption = plot_Caption) +
  theme_lineplot()

lineplot
# Export -------------------------------------------------------------------

theme_laptop <- function(){

font_size_Title <- size_XXL
font_size_Subtitle <- size_XL
font_size_Caption <- size_L
font_size_Axis <- size_L
update_geom_defaults("label", list(family = font_family_Primary, size = 4.5))

theme(
plot.title = element_text(size = font_size_Title, margin = unit(c(size_XXL, 0, size_XL, 0), "pt")),
plot.subtitle = element_text(size = font_size_Subtitle),
plot.caption = element_text(size = font_size_Caption),
axis.text = element_text(size = font_size_Axis)
)
}

theme_tablet <- function(){

font_size_Title <- size_XL
font_size_Subtitle <- size_L
font_size_Caption <- size_M
font_size_Axis <- size_M
update_geom_defaults("label", list(family = font_family_Primary, size = 3 ))

theme(
plot.title = element_text(size = font_size_Title),
plot.subtitle = element_text(size = font_size_Subtitle, margin = unit(c(0, 0, size_XXL, 0), "pt")),
plot.caption = element_text(size = font_size_Caption),
axis.text = element_text(size = font_size_Axis)
)
}

theme_mobile <- function(){

font_size_Title <- size_XL
font_size_Subtitle <- size_L
font_size_Caption <- size_M
font_size_Axis <- size_M
update_geom_defaults("label", list(family = font_family_Secondary, size = 2.5))

theme(
plot.title = element_text(size = font_size_Title),
plot.subtitle = element_text(size = font_size_Subtitle),
plot.caption = element_text(size = font_size_Caption, hjust = 0.5),
axis.text = element_text(size = font_size_Axis),
axis.text.y = element_text(family = font_family_Secondary)
)
}

ppi <- 72
dim_laptop <- c(1120, 540)
dim_tablet <- c(608, 342)
dim_mobile <- c(320, 320)

ggsave(lineplot + theme_laptop(), width = dim_laptop[1]/ppi, height = dim_laptop[2]/ppi, units = "in", device = "svg", filename = paste("export/", plot_Title, "-laptop.svg", sep = ""))
ggsave(lineplot + theme_tablet(), width = dim_tablet[1]/ppi, height = dim_tablet[2]/ppi, units = "in", device = "svg", filename = paste("export/", plot_Title, "-tablet.svg", sep = ""))
ggsave(lineplot + theme_mobile(), width = dim_mobile[1]/ppi, height = dim_mobile[2]/ppi, units = "in", device = "svg", filename = paste("export/", plot_Title, "-mobile.svg", sep = ""))