In the section on further customization, themes are discussed.
For the plot we have made for the Beatles some themes are experimented with.
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.3 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(mdsr)
library(babynames)
Beatles <- babynames %>%
filter(name %in% c("John", "Paul", "George", "Ringo") & sex == "M")
beatles_plot <- Beatles %>% ggplot(aes(x = year, y = n)) +
geom_line(aes(color = name), size = 2)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
beatles_plot + theme_grey()
beatles_plot + theme_bw()
beatles_plot + theme_minimal()
beatles_plot + theme_classic()
beatles_plot + theme(panel.background = element_rect(fill = "cornsilk"),
panel.grid.major = element_line(color = "dodgerblue"))
The theme used in the book.
Note the use of **%+replace%*.
theme_mdsr <- function(base_size = 12, base_family = "Bookman"){
theme_grey(base_size = base_size, base_family = base_family) %+replace%
theme(
axis.text = element_text(size = rel(0.8)),
axis.ticks = element_line(colour = "black"),
legend.key = element_rect(colour = "grey80"),
panel.background = element_rect(fill = "whitesmoke", colour = NA),
panel.border = element_rect(fill = NA, colour = "grey50"),
panel.grid.major = element_line(colour = "grey80", size = 0.2),
panel.grid.minor = element_line(colour = "grey92", size = 0.5),
strip.background = element_rect(fill = "grey80", colour = "grey50", size = 0.2)
)
}
beatles_plot + theme_mdsr()
Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
beatles_plot + facet_wrap(~name) + theme_mdsr()
Load the package ggthemes.
library(ggthemes)
beatles_plot + facet_wrap(~name) + theme_solarized()
beatles_plot + facet_wrap(~name) + theme_tufte()
beatles_plot + facet_wrap(~name) + theme_fivethirtyeight()
beatles_plot + facet_wrap(~name) + theme_excel()
Here is the xkcd web comic.
To get started with the xkcd fonts you start by reading the vignette.
Run this code once.
download.file("http://simonsoftware.se/other/xkcd.ttf",
dest="xkcd.ttf", mode="wb")
system("mkdir ~/.fonts")
system("cp xkcd.ttf ~/.fonts")
font_import(pattern = "[X/x]kcd", prompt=FALSE)
fonts()
fonttable()
Loading required package: extrafont
library(extrafont)
beatles_plot + theme_xkcd()
Warning in theme_xkcd(): Not xkcd fonts installed! See vignette("xkcd-intro")
Hand drawn axis.
xrange <- range(mtcars$mpg)
yrange <- range(mtcars$wt)
set.seed(123) # for reproducibility
p <- ggplot() + geom_point(aes(mpg, wt), data=mtcars) +
xkcdaxis(xrange,yrange)
Warning in theme_xkcd(): Not xkcd fonts installed! See vignette("xkcd-intro")
Comic looking plots.
ratioxy <- diff(xrange)/diff(yrange)
mapping <- aes(x, y, scale, ratioxy, angleofspine,
anglerighthumerus, anglelefthumerus,
anglerightradius, angleleftradius,
anglerightleg, angleleftleg, angleofneck,
linetype=city)
Warning: Duplicated aesthetics after name standardisation:
dataman <- data.frame(x= c(15,30), y=c(3, 4),
scale = c(0.3,0.51) ,
ratioxy = ratioxy,
angleofspine = -pi/2 ,
anglerighthumerus = c(pi/4, -pi/6),
anglelefthumerus = c(pi/2 + pi/4, pi +pi/6),
anglerightradius = c(pi/3, -pi/3),
angleleftradius = c(pi/3, -pi/3),
anglerightleg = 3*pi/2 - pi / 12,
angleleftleg = 3*pi/2 + pi / 12 ,
angleofneck = runif(1, 3*pi/2-pi/10, 3*pi/2+pi/10),
city=c("Liliput","Brobdingnag"))
p <- ggplot() + geom_point(aes(mpg, wt, colour=as.character(vs)), data=mtcars) +
xkcdaxis(xrange,yrange) +
xkcdman(mapping, dataman)
Warning in theme_xkcd(): Not xkcd fonts installed! See vignette("xkcd-intro")
One more dynamic graphics library, that is from Python, R bokeh.
Run the following code once.
# from github:
# remotes::install_github("bokeh/rbokeh")
library(rbokeh)
p1 <- figure(width = 800, height = 400) %>%
ly_points(year, n, data = Beatles, color = name, glyph = name, hover = list(year, n, name))
p1
p2 <- figure(width = 800, height = 400) %>%
ly_lines(x = year, y = n, group = name, data = Beatles, color = name, hover = list(year, n, name))
p2