-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
144 lines (97 loc) · 2.02 KB
/
README.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
---
title: "README"
author: "Daniel Chen"
date: ""
output:
md_document:
variant: markdown_github
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Reference Links
http://www.cookbook-r.com/Graphs/
http://ggplot2.tidyverse.org/reference/
```{r}
library(ggplot2)
diamonds
```
# Base Graphics
## 1 variable continuous
```{r}
hist(diamonds$price)
```
# ggplot2
```{r}
ggplot()
```
## 1 variable continuous
```{r}
ggplot(data = diamonds, mapping = aes(x = price)) + geom_histogram()
```
```{r}
ggplot(data = diamonds, mapping = aes(x = price)) + geom_density()
```
## 1 variable discrete
```{r}
ggplot(data = diamonds, mapping = aes(x = cut)) + geom_bar()
```
## 2 variables discrete/continuous
```{r}
ggplot(diamonds, aes(x = cut, y = price)) + geom_boxplot()
ggplot(diamonds, aes(x = cut, y = price)) + geom_violin()
```
## 2 variables continuous/continuous
```{r}
ggplot(diamonds, aes(x = carat, y = price)) + geom_point()
```
## Layering in ggplot
```{r}
ggplot(diamonds) +
geom_point(aes(x = carat, y = price)) +
geom_hline(yintercept = 10000)
```
## Colors
```{r}
ggplot(diamonds, aes(x = carat, y = price)) + geom_point(color = "blue")
```
```{r}
ggplot(diamonds, aes(x = carat, y = price, color = color)) + geom_point()
```
## Facet wrap
```{r}
ggplot(diamonds, aes(x = carat, y = price, color = color)) +
geom_point() +
facet_wrap(~cut)
```
## Facet grid
```{r}
ggplot(diamonds, aes(x = carat, y = price, color = color)) +
geom_point() +
facet_grid(clarity~cut)
```
## Saving ggplots as a variable
```{r}
g <- ggplot(diamonds, aes(x = carat, y = price, color = color)) +
geom_point()
g
g + facet_grid(clarity ~ cut)
```
## Alpha transparency
```{r}
ggplot(diamonds, aes(x = carat, y = price, color = color)) + geom_point(alpha = .1)
```
## Hexbin
```{r}
ggplot(diamonds, aes(x = carat, y = price, fill = color)) + geom_hex()
```
# Themes
```{r}
library(ggthemes)
g + theme_bw()
g + theme_minimal()
g + theme_economist()
g + theme_wsj()
g + theme_excel()
```