Skip to content
Newer
Older
100644 60 lines (44 sloc) 2.49 KB
b283b3e @thercast Adding code used in ggplot2 episode
authored Jun 19, 2012
1 # load hockey data analysis project
2
3 library(ProjectTemplate)
4 load.project()
5
6 #-------------------------------------------------------------------------------------
7 # create combination of box plot, violin plot using ggplot2 to visualize attendance
8 # numbers
9 # - subset to only include games not played in outdoor stadiums and years 1994-2012
10 #-------------------------------------------------------------------------------------
11
12 plot.data <- subset(reg.game, subset=(attendance < 30000 & year >= 1994))
13
14 library(ggplot2)
15
16 myplot <- ggplot(data=plot.data, aes(x=home.team, y=attendance))
17
18 myplot
19
20 summary(myplot)
21
22 myplot + geom_boxplot(aes(fill=home.conference), width=0.3, outlier.colour=NA)
23
24 last_plot() + coord_cartesian(ylim=c(10000, 23000))
25
26 last_plot() + theme_bw()
27
28 last_plot() + labs(x="Team", y="Attendance", fill="Conference")
29
30 last_plot() + opts(title = "Distributions of Game Attendance",
31 plot.title = theme_text(size=20),
32 legend.position="top",
33 legend.title = theme_text(size=20),
34 legend.text = theme_text(size=12),
35 axis.text.x = theme_text(angle=90, size=12),
36 axis.title.x = theme_text(size=20),
37 axis.text.y = theme_text(size=12),
38 axis.title.y = theme_text(angle=90, size=16),
39 strip.text.y = theme_text(angle=90, size=12))
40
41 last_plot() + facet_grid(era.ind ~ ., margins=FALSE)
42
43 myplot.labs <- labs(x="Team", y="Attendance", fill="Conference", colour="Conference")
44 myplot.theme <- list(theme_bw(),
45 opts(title = "Distributions of Game Attendance"),
46 opts(plot.title = theme_text(size=20)),
47 opts(legend.position="top"),
48 opts(legend.title = theme_text(size=20)),
49 opts(legend.text = theme_text(size=12)),
50 opts(axis.text.x = theme_text(angle=90, size=12)),
51 opts(axis.title.x = theme_text(size=20)),
52 opts(axis.text.y = theme_text(size=12)),
53 opts(axis.title.y = theme_text(angle=90, size=16)),
54 opts(strip.text.y = theme_text(angle=90, size=12)))
55 myplot.zoom <- coord_cartesian(ylim=c(10000, 23000))
56 myplot.box <- geom_boxplot(aes(fill=home.conference), width=0.3, outlier.colour=NA)
57
58 myplot <- ggplot(data=plot.data, aes(x=home.team, y=attendance))
59 myplot + myplot.box + myplot.zoom + myplot.labs + myplot.theme + facet_grid(era.ind ~ ., margins=FALSE)
Something went wrong with that request. Please try again.