--- title: "ggplot2 themes" author: "Prof. Eric A. Suess" format: html: self-contained: true --- In the section on further customization, themes are discussed. For the plot we have made for the Beatles some themes are experimented with. ```{r} library(tidyverse) 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) beatles_plot ``` ```{r} beatles_plot + theme_grey() ``` ```{r} beatles_plot + theme_bw() ``` ```{r} beatles_plot + theme_minimal() ``` ```{r} beatles_plot + theme_classic() ``` ```{r} 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%*. ```{r} 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() ``` ```{r} beatles_plot + facet_wrap(~name) + theme_mdsr() ``` Load the package *ggthemes*. ```{r} library(ggthemes) beatles_plot + facet_wrap(~name) + theme_solarized() ``` ```{r} beatles_plot + facet_wrap(~name) + theme_tufte() ``` ```{r} beatles_plot + facet_wrap(~name) + theme_fivethirtyeight() ``` ```{r} beatles_plot + facet_wrap(~name) + theme_excel() ``` Here is the [xkcd](https://xkcd.com/) web comic. To get started with the *xkcd* fonts you start by reading the vignette. ```{r} vignette("xkcd-intro") ``` Run this code once. ```{r} #| eval=FALSE 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() ``` ```{r} library(xkcd) library(extrafont) beatles_plot + theme_xkcd() ``` Hand drawn axis. ```{r} 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) p ``` Comic looking plots. ```{r} ratioxy <- diff(xrange)/diff(yrange) mapping <- aes(x, y, scale, ratioxy, angleofspine, anglerighthumerus, anglelefthumerus, anglerightradius, angleleftradius, anglerightleg, angleleftleg, angleofneck, linetype=city) 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) p ``` One more dynamic graphics library, that is from Python, R bokeh. Run the following code once. ```{r} # from github: # remotes::install_github("bokeh/rbokeh") ``` ```{r} 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 ```