Commit f3a0323e authored by Graham's avatar Graham

Initial import

parent 92408806
########################################################################
# Introduce the concept of faceted plots
#
# Copyright 2019 Graham.Williams@togaware.com
library(mlhub)
mlcat("Visualisaing Data Using Faceted Plots",
"A powerful mechanism for exposing the story bewhind some data is through the
use of faceted plots. Such plots are appropriate when there is a natural
partitioning of the data usually through a categoric variables.")
# Load required packages.
suppressMessages(
{
library(ggplot2)
library(magrittr)
})
mlask()
mlcat("Nobel Laureates",
"This example comes from a post by Emily Klarquist on Twitter, 20 May 2019,
https://t.co/kJzwyJg0x0. It uses data reporting on the Nobel winners over
the years for the various prizes, from Nobelprize.org. Here the facets are
the different categories. The plot mimics a plot published originally in the
Economist (Econmist.com).")
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-05-14/nobel_winners.csv" %>%
read.csv() ->
nobel
nobel$birth_date %>%
substring(1, 4) %>%
as.integer() ->
nobel$birth_year
nobel$award_age <- (nobel$prize_year - nobel$birth_year)
nobel$category %<>% ordered(levels = c("Medicine", "Physics", "Chemistry",
"Economics" , "Literature", "Peace"))
levels(nobel$category)[4] <- "Economics*"
mlask()
fname <- "facet_nobel.pdf"
pdf(fname, width=12)
nobel %>%
ggplot(aes(x=prize_year, y=award_age, color=category)) +
geom_point(shape=1, size=1) +
facet_grid(cols=vars(category)) +
geom_smooth(aes(color=category), se=FALSE, lwd=1.5) +
labs(title='Senescience',
subtitle='Age of Nobel laureates, at date of award',
caption='Source: Nobelprize.org *the economics prize was first awarded in 1969') +
theme(plot.title=element_text(face = "bold"),
legend.position="none",
strip.text.x=element_text(angle=0,
hjust=0,
face="bold",
color=c("#014d64", "#90353B", "#EE6A50",
"#2D6D66", "#EE9A00", "#01A2D9"))) +
theme(plot.caption=element_text(hjust=0, size=9)) +
scale_x_continuous(name= "",
breaks=c(1900, 1925, 1950, 1975, 2000),
labels=c(1900, "", 50, "", 2000),
limits=c (1900, 2025)) +
scale_y_continuous(name="",
breaks=c(25, 50, 75, 100), labels=c(25, 50, 75, 100),
position="right",
25,
limits=c(15, 101)) +
theme(panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank() ,) +
scale_colour_manual(values=c("#014d64", "#90353B", "#EE6A50",
"#2D6066", "#EE9A00", "#01A209")) +
annotate("text", x=Inf, y=96, label ="Oldest winner \n Leonid Hurwicz, 90", hjust=1, size=2.5,
colour=c ("grey92", "grey92", "grey92", "#2D6066", "grey92", "grey92")) +
annotate("text", x= Inf, y= 25, label = "Youngest winner \n Malala Yousafzai, 17", hjust = 1, size = 2.5,
colour = c ("grey92", "grey92", "grey92", "grey92", "grey92", "#01A209"))
invisible(dev.off())
mlpreview(fname)
mlask()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment