-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggthemes.Rmd
128 lines (102 loc) · 2.98 KB
/
ggthemes.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
---
title: "ggthemes"
author: "Elizabeth Borgognoni"
date: "March 3, 2017"
output:
html_document: default
pdf_document: default
---
To run this tutorial, you'll need:
```{r, eval=FALSE}
install.packages(c('ggplot2', 'grid', 'gridExtra', 'ggthemes', 'dplyr'))
```
Loading useful packages
- The `ggplot2` package is one of the best packages for the production of high-quality plots.
```{r, warning = FALSE}
library(ggplot2)
library(grid)
library(gridExtra)
library(ggthemes)
library(dplyr)
```
Create my theme
```{r}
my_theme<-function(base_size=14, base_family="Lucia") {
library(grid)
library(ggthemes)
(theme_foundation(base_size= base_size, base_family=base_family)+
theme(plot.margin=unit(c(5,5,10,5),"mm"),
plot.background = element_rect(fill = "white", colour = "black", size = 1),
plot.title = element_text(face = "bold", size = rel(1.2), hjust = 0.5),
axis.title = element_text(face = "bold",size = rel(1)),
axis.title.y = element_text(angle=90,vjust =2),
axis.title.x = element_text(vjust = -0.2),
axis.text = element_text(),
axis.line = element_line(colour="black"),
axis.ticks = element_line(),
panel.grid.major = element_line(colour="#f0f0f0"),
panel.grid.minor = element_line(colour = "#f0f0f0"),
legend.position = "bottom",
legend.direction = "horizontal",
legend.key.size= unit(0.2, "cm"),
legend.spacing = unit(0, "cm"),
legend.title = element_text(face="italic"),
strip.background=element_rect(colour="#f0f0f0",fill="#f0f0f0"),
strip.text = element_text(face="bold")
))
}
scale_fill<-function(){
scale_fill_brewer(palette="Spectral")
}
scale_colour<-function(){
scale_colour_brewer(palette="PRGn")
}
```
Making barplots with differents themes
```{r}
Barplot<-ggplot(mtcars, aes(factor(carb),fill=factor(carb))) +
geom_bar(alpha=0.7) +
ggtitle("Bar Plot") +
my_theme()
Barplot
library(gridExtra)
#scales
grid.arrange(Barplot,(Barplot + scale_fill()))
```
Making scatter plots with differents themes
```{r}
library("dplyr")
set.seed(1234)
centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2))
points <- centers %>% group_by(cluster) %>%
do(data.frame(x1=rnorm(.$size[1], .$x1[1]),
x2=rnorm(.$size[1], .$x2[1])))
ggplot(points, aes(x1, x2, color=cluster)) +
geom_point(size = 2) +
ggtitle("Multivariate gaussian") +
my_theme()
```
Making bubble plots with differents themes
```{r}
Bubble<-ggplot(mtcars, aes(mpg,disp,colour=factor(carb),size=hp)) +
geom_point(alpha=0.7) +
ggtitle("Bubble Plot") +
scale_size_continuous(range = c(3,10)) +
my_theme()
Bubble
library(gridExtra)
#scales
grid.arrange(Bubble,(Bubble + scale_colour()))
```
Making regression plots with differents themes
```{r}
set.seed(1234)
dat <- data.frame(cond = rep(c("A", "B"), each=50),
xvar = 1:50 + rnorm(50,sd=3),
yvar = 1:50 + rnorm(50,sd=3))
ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) +
geom_smooth(method=lm) +
ggtitle("Linear Regression")+
my_theme()
```