demo.R 3.42 KB
Newer Older
Graham's avatar
Graham committed
1 2 3 4 5 6 7 8 9
########################################################################
# 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
10 11 12
use of faceted plots. A faceted plot repeats a particular plot style but for
different subsets of the data. Such plots are appropriate when there is a
natural partitioning of the data usually through a categoric variable.")
Graham's avatar
Graham committed
13 14 15 16 17 18 19

# Load required packages.

suppressMessages(
{
  library(ggplot2)
  library(magrittr)
20
  library(tidyr)
Graham's avatar
Graham committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
})

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

41 42
nobel$award_age <- ifelse(is.na(nobel$birth_year), 0,
                          nobel$prize_year - nobel$birth_year)
Graham's avatar
Graham committed
43 44 45 46 47 48 49 50

nobel$category %<>% ordered(levels = c("Medicine", "Physics", "Chemistry",
                                       "Economics" , "Literature", "Peace"))

levels(nobel$category)[4] <- "Economics*"

mlask()

Graham's avatar
Graham committed
51
fname <- "facets_nobel.pdf"
Graham's avatar
Graham committed
52 53 54 55 56
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)) +
57
  geom_smooth(aes(color=category), se=FALSE, lwd=1.5, method="loess") +
Graham's avatar
Graham committed
58 59 60 61 62 63 64 65 66 67 68 69 70
  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),
71
                     labels=c(1900, "", 1950, "", 2000),
Graham's avatar
Graham committed
72
                     limits=c (1900, 2025)) +
73 74 75 76 77
#  scale_y_continuous(name="",
#                     breaks=c(25, 50, 75, 100), labels=c(25, 50, 75, 100),
#                     position="right",
#                     25,
#                     limits=c(15, 101)) +
Graham's avatar
Graham committed
78 79 80
  theme(panel.grid.major.x=element_blank(),
        panel.grid.minor.x=element_blank() ,) +
  scale_colour_manual(values=c("#014d64", "#90353B", "#EE6A50",
81 82 83 84 85
                               "#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"))
86 87
plot
suppressWarnings(print(plot))
Graham's avatar
Graham committed
88 89 90 91 92
invisible(dev.off())

mlpreview(fname)

mlask()