/
192-ggplot-themes.Rmd
173 lines (114 loc) · 5.47 KB
/
192-ggplot-themes.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
---
title: "Available theme for ggplot2"
descriptionMeta: "A set of pre-built theme are available to quickly customize a ggplot2 chart. This post aims to give an overview of the available themes, provided by ggplot2 itself or by other packages."
descriptionTop: "A set of pre-built theme are available to quickly customize a `ggplot2` chart. This post aims to give an overview of the available themes, provided by `ggplot2` itself or by other packages."
sectionText: "ggplot2 section"
sectionLink: "ggplot2-package.html"
DataToVizText: "Data to Viz"
DataToVizLink: "data-to-viz.com"
url: "192-ggplot-themes"
output:
html_document:
self_contained: false
mathjax: default
lib_dir: libs
template: template_rgg.html
css: style.css
toc: TRUE
toc_float: TRUE
toc_depth: 2
df_print: "paged"
---
```{r global options, include = FALSE}
knitr::opts_chunk$set( warning=FALSE, message=FALSE)
```
<div class="container">
# Themes provided by `ggplot2`
***
<div class = "row">
<div class = "col-md-6 col-sm-12 ">
```{r thecode3a, echo=FALSE, out.width = "80%", fig.height=6, fig.align="center"}
# library
library(ggplot2)
library(gridExtra)
# create data
set.seed(123)
var=rnorm(1000)
# Without theme
plot1 <- qplot(var , fill=I(rgb(0.1,0.2,0.4,0.6)) )
# With themes
plot2 = plot1+theme_bw()+annotate("text", x = -1.9, y = 75, label = "bw()" , col="orange" , size=4)
plot3 = plot1+theme_classic()+annotate("text", x = -1.9, y = 75, label = "classic()" , col="orange" , size=4)
plot4 = plot1+theme_gray()+annotate("text", x = -1.9, y = 75, label = "gray()" , col="orange" , size=4)
plot5 = plot1+theme_linedraw()+annotate("text", x = -1.9, y = 75, label = "linedraw()" , col="orange" , size=4)
plot6 = plot1+theme_dark()+annotate("text", x = -1.9, y = 75, label = "dark()" , col="orange" , size=4)
plot7 = plot1+theme_get()+annotate("text", x = -1.9, y = 75, label = "get()" , col="orange" , size=4)
plot8 = plot1+theme_minimal()+annotate("text", x = -1.9, y = 75, label = "minimal()" , col="orange" , size=4)
# Arrange and display the plots into a 2x1 grid
grid.arrange(plot1,plot2,plot3,plot4, ncol=2)
```
</div>
<div class = "col-md-6 col-sm-12">
```{r thecode3b, echo=FALSE, out.width = "80%", fig.height=6, fig.align="center"}
grid.arrange(plot5,plot6,plot7,plot8, ncol=2)
```
</div>
</div>
Two main types of grid exist with `ggplot2`: major and minor. They are controled thanks to the `panel.grid.major` and `panel.grid.minor` options.
Once more, you can add the options `.y` or `.x` at the end of the function name to control one orientation only.
Features are wrapped in an `element_line()` function. Specifying `element_blanck()` will simply removing the grid.
```{r thecode3a, eval=FALSE}
```
```{r thecode3b, eval=FALSE}
```
# Code used for the `ggplot2` page.
***
Here is the code used for the examples displayed on the [ggplot2 page](ggplot2-package.html). It shows how to used most of the common packages related to `ggplot2` themes.
[See how themes look like](ggplot2-package.html#themes).
```{r, eval=FALSE}
# library
library(ggplot2)
# My margini
mytheme <- theme(
plot.margin=unit(rep(1.3,4),"cm")
)
# Plot
p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) +
geom_point(size=6, alpha=0.6, show.legend = FALSE)
setwd("~/Desktop/R-graph-gallery/img/graph")
png("192-ggplot-theme_default.png") ; p + mytheme ; dev.off()
png("192-ggplot-theme_bw.png") ; p + theme_bw() + mytheme ; dev.off()
png("192-ggplot-theme_minimal.png") ; p + theme_minimal() + mytheme ; dev.off()
png("192-ggplot-theme_classic.png") ; p + theme_classic() + mytheme ; dev.off()
png("192-ggplot-theme_gray.png") ; p + theme_gray() + mytheme ; dev.off()
#library(ggthemes)
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
png("192-ggplot-theme_economist.png") ; p + theme_economist() + mytheme ; dev.off()
png("192-ggplot-theme_fivethirtyeight.png") ; p + theme_fivethirtyeight() + mytheme ; dev.off()
png("192-ggplot-theme_tufte.png") ; p + theme_tufte() + mytheme ; dev.off()
png("192-ggplot-theme_gdocs.png") ; p + theme_gdocs() + mytheme ; dev.off()
png("192-ggplot-theme_wsj.png") ; p + theme_wsj() + mytheme ; dev.off()
png("192-ggplot-theme_calc.png") ; p + theme_calc() + scale_colour_calc() + mytheme ; dev.off()
png("192-ggplot-theme_hc.png") ; p + theme_hc() + scale_colour_hc() + mytheme ; dev.off()
# library(hrbrthemes)
png("192-ggplot-theme_ipsum.png") ; p + theme_ipsum() + scale_color_ipsum() + mytheme ; dev.off()
# library(egg)
png("192-ggplot-theme_article.png") ; p + theme_article() + mytheme ; dev.off()
# library(ggpubr)
png("192-ggplot-theme_pubclean.png") ; p + theme_pubclean() + mytheme ; dev.off()
# library(bigstatsr)
png("192-ggplot-theme_bigstatsr.png") ; p + theme_bigstatsr() + mytheme ; dev.off()
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
png("192-ggplot-theme_excel.png") ; p + theme_excel() + mytheme ; dev.off()
theme_bigstatsr()
```
<!-- Close container -->
</div>
```{r, echo=FALSE}
htmltools::includeHTML("htmlChunkRelatedPartOfWhole.html")
```