-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggplotTutorial.R
More file actions
225 lines (166 loc) · 5.69 KB
/
ggplotTutorial.R
File metadata and controls
225 lines (166 loc) · 5.69 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
########################
# ggplot2 Demo and Instruction
# Brian J. Enquist
# February 22 2015
#
#######################
# see also http://naupaka.github.io/2015-01-10-st_josephs/workshop/02_Sunday/04_Sunday_pm/ggplot.pdf
# this is the script way of installing packages
install.packages ("ggplot2", dependencies = TRUE)
install.packages("plyr")
install.packages("ggthemes")
install.packages("reshape2")
#load libraries
library(ggplot2)
library(reshape2)
library(ggthemes)
library(plyr)
#what are the data and how do we map them
myplot <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width))
summary(myplot)
myplot + geom_point()
myiris <- iris
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point()
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) +
geom_point(aes(shape = Species), size =3)
## Exercise 1
d2 <- diamonds[sample(1:dim(diamonds)[1],1000),]
dim(diamonds)
dim(diamonds)[1]
#learn more http://docs.ggplot2.org/current/
#http://docs.ggplot2.org/current/
# done with lecture
head(d2)
myplot2 <- ggplot(data=d2, aes(x = carat, y = price))
summary(myplot2)
myplot2 + geom_point(aes(color = color), size =6)
#myplot2 + geom_point(aes(shape = color), size =3)
#another way
ggplot(d2, aes(carat, price, color=color)) + geom_point()
library(MASS)
ggplot(birthwt, aes(factor(race), bwt))+ geom_boxplot()
head(birthwt)
myplot3 <- ggplot(birthwt, aes(factor(race), bwt))+ geom_boxplot()
summary(myplot3) #looking inside the ggplot object
ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) +
geom_point() +
facet_grid(Species ~ .) # dot is saying there is no other access that we want to subset
# note y axis is shared and the x axis is repeated
# or
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
facet_grid(. ˜ Species)
#or
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
facet_wrap(~ Species)
#
library(RColorBrewer)
display.brewer.all()
#using melt
df <-melt(iris, id.vars = "Species") #click on df in the environment tab because we have melted it we can map it
ggplot(df, aes(Species, value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") + # dodge is putting all four measures together
scale_fill_brewer(palette = "Set1")
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
ffacet_grid(Species ˜ .) +
scale_color_manual(values = c("red", "green", "blue"))
library(MASS)
ggplot(birthwt, aes(factor(race), bwt)) +
geom_boxplot(width = .2) +
scale_y_continuous(labels = (paste (1:4, " Kg")),
breaks = seq(1 , 4 , by = 1 ))
#histogram examples
h <- ggplot(faithful, aes(x = waiting))
h + geom_histogram(binwidth = 30, colour = "black")
h <- ggplot(faithful, aes(x = waiting))
h + geom_histogram(binwidth = 8, fill = "steelblue",
colour = "black")
#Lines
# Bar Plots
ggplot(iris, aes(Species, Sepal.Length)) +
geom_bar(stat = "identity")
df <- melt(iris, id.vars = "Species")
ggplot(df, aes(Species, value, fill = variable)) +
geom_bar(stat = "identity")
ggplot(df, aes(Species, value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge")
ggplot(df, aes(Species, value, fill = variable)) +
geom_bar(stat = "identity", position="dodge", color="black")
### Density
ggplot(faithful, aes(waiting)) + geom_density()
ggplot(faithful, aes(waiting)) +
geom_density(fill = "blue", alpha = 0.1)
ggplot(faithful, aes(waiting)) +
geom_line(stat = "density")
## Adding smoothers
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(aes(shape = Species), size = 2) +
geom_smooth(method = "lm")
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(aes(shape = Species), size = 3) +
geom_smooth(method = "lm") +
facet_grid(. ~ Species)
##Adding Themes
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3, shape = 16) +
facet_wrap(~Species) +
theme_bw()
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 1.2, shape = 16) +
facet_wrap(~Species) +
theme(legend.key = element_rect(fill = NA),
legend.position = "bottom",
strip.background = element_rect(fill = NA),
axis.title.y = element_text(angle = ))
install.packages(ggthemes)
library(ggthemes)
#read more http://cran.r-project.org/web/packages/ggthemes/vignettes/ggthemes.html
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3, shape = 16) +
facet_wrap(~Species) +
#theme_excel()
#theme_wsj()
#theme_solarized()
#theme_stata()
#theme_economist()
#theme_fivethirtyeight()
#theme_tufte()
#theme_economist()
theme_pander() #not bad
#theme_calc() #pretty good
## customize
my_custom_plot <- function(df, title = "", ...) {
ggplot(df, ...) +
ggtitle(title) +
whatever_geoms() +
theme(...)
}
plot1 <- my_custom_plot(iris, title = "Figure 1")
## publication quality figures
ggsave(˜/Desktop/filename.png)
ggsave(file = "/path/to/figure/filename.png", width = 6,
height =4)
ggsave(file = "/path/to/figure/filename.eps")
ggsave(file = "/path/to/figure/filename.jpg")
ggsave(file = "/path/to/figure/filename.pdf")
'
'
#### Done with lecture
#how to visualize them
head(iris)
ddply(iris, .(Species), summarize)
df <- melt (iris, id)
dcast(df, Species ~ variable, mean)
#i want hue will help you pick a palet
# http:// tools.medialab.sciences-po.fr/iwanthue/
## unfinished
ggplot(birthwt, aes(factor(race), bwt)) +
geom_boxplot(width = .2) +
scale_y_continuous(labels = (pasteO(1:4, "Kg")),
breaks )
##