-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggthemes_present.Rmd
107 lines (86 loc) · 4.21 KB
/
ggthemes_present.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
title: "ggplot2_themes_summary"
author: "Yangjw"
date: "2020年12月12日"
output: rmarkdown::github_document
---
```{r, include=F}
# knitr::opts_chunk$set(fig.path="ggplot2_themes_figs/themes-",include=FALSE)
Sys.setlocale('LC_ALL','C')
suppressPackageStartupMessages(library(extrafont))
loadfonts(device = "win")
```
## 0. ggplot2 default.
```{r,fig.width=12,fig.height=10,warning=F}
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(patchwork))
suppressPackageStartupMessages(library(purrr))
df <- data.frame( x=1:50, y=rnorm(50,mean=5,sd=1), z=sample(x=LETTERS[1:3],50,replace = T) )
scale.colors <- sample(c("brewer","discrete","hue","ordinal","viridis_d"), size = 10, replace = T)
element.names <- c("bw","classic","dark","gray","grey","light","linedraw","minimal","test","void")
map.ggplot2 <- map2( element.names, scale.colors, ~{
# input.colors <- ifelse( exists(paste0("scale_color_",.)), paste0("scale_color_",.),"scale_color_brewer" )
ggplot(df,aes(x,y,color=z))+geom_point()+
eval(parse(text=paste0("scale_color_",.y,"()")))+
eval(parse(text=paste0("theme_",.x,"()")))+
labs(title=paste0("Themes: ",.x,"\ncolors: ",.y))
})
# get, set, update and replace
## get - return current theme settings
# theme_get()$plot.margin
## set - set the current theme
p <- map.ggplot2[[10]]
old <- theme_set(theme_bw())
## update
theme_set(old)
theme_update(panel.grid.minor = element_line(colour = "red"))
map.ggplot2[[11]] <- p+old+labs(title = "Usage: update")
## replace
theme_set(old)
theme_replace(panel.grid.minor = element_line(colour = "blue"))
map.ggplot2[[12]] <- p+old+labs(title = "Usage: replace")
map.ggplot2[[13]] <- map.ggplot2[[12]]+theme_grey() %+replace%
theme(text = element_text(family = "mono"))+labs(title = "Usage: %+replace%")
wrap_plots( map.ggplot2, ncol = 4 )
```
## 1. ggthemes
```{r,fig.width=30,fig.height=25,warning=F}
suppressPackageStartupMessages(library(ggthemes))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(patchwork))
suppressPackageStartupMessages(library(purrr))
df <- data.frame( x=1:50, y=rnorm(50,mean=5,sd=1), z=sample(x=LETTERS[1:3],50,replace = T) )
element.names <- c("base","calc","clean","economist","excel","excel_new","few","fivethirtyeight","foundation","gdocs","hc","igray","map","pander","par","solarized","solid","stata","tufte","wsj")
map.ggthemes <- map( element.names, ~{
input.colors <- ifelse( exists(paste0("scale_color_",.)), paste0("scale_color_",.),"scale_color_brewer" )
ggplot(df,aes(x,y,color=z))+geom_point()+eval(parse(text=paste0(input.colors,"()")))+ eval(parse(text=paste0("theme_",.,"()")))+labs(title=paste0("Themes: ",.,"\ncolors: ",input.colors))
})
wrap_plots( map.ggthemes, ncol = 5 )
```
## 2. ggpubr
```{r,fig.width=8,fig.height=4,warning=F}
suppressPackageStartupMessages(library(ggpubr))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(patchwork))
suppressPackageStartupMessages(library(purrr))
df <- data.frame( x=1:50, y=rnorm(50,mean=5,sd=1), z=sample(x=LETTERS[1:3],50,replace = T) )
element.names <- c("pubr","transparent")
map.ggpubr <- map( element.names, ~{
ggplot(df,aes(x,y,color=z))+geom_point()+ eval(parse(text=paste0("theme_",.,"()")))+labs(title=paste0("Themes: ",.))
})
wrap_plots( map.ggpubr, ncol = 2 )
```
## 3. hrbrthemes
```{r,fig.height=10,fig.width=25,warning=F}
suppressPackageStartupMessages(library(ggsci))
suppressPackageStartupMessages(library(hrbrthemes))
suppressPackageStartupMessages(library(patchwork))
suppressPackageStartupMessages(library(purrr))
df <- data.frame( x=1:50, y=rnorm(50,mean=5,sd=1), z=sample(x=LETTERS[1:3],50,replace = T) )
scale.color <- sample(c("ipsum","ft"), size = 8, replace = T)
element.names <- c("ipsum","ipsum_es","ipsum_rc","ipsum_ps","ipsum_pub","ipsum_tw","modern_rc","ft_rc")
map.hrbrthemes <- map2( element.names, scale.color, ~{
ggplot(df,aes(x,y,color=z))+geom_point()+eval(parse(text=paste0("scale_color_",.y,"()")))+ eval(parse(text=paste0("theme_",.x,"()")))+labs(title=paste0("Themes: ",.x,"\ncolors: ",.y))
})
wrap_plots( map.hrbrthemes, ncol = 4 )
```