forked from GreatKarollo/code_r
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleveland-plot.r
More file actions
52 lines (44 loc) · 1.79 KB
/
cleveland-plot.r
File metadata and controls
52 lines (44 loc) · 1.79 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
# Cleveland Plot
# original source: r graphics cookbook
# load the ggplot2 library
library(ggplot2)
# load the gcookbook library
library(gcookbook)
# create an object that is the top 25 hitters of 2001
tophit <- tophitters2001[1:25, ]
# get the names, sorted first by lg, then by avg
nameorder <- tophit$name[order(tophit$lg, tophit$avg)]
#order the factor variable in the order of nameorder
tophit$name <- factor(tophit$name, levels=nameorder)
# create the ggplot data
ggplot(tophit, aes(x=avg, y=reorder(name, avg))) +
# use a larger dot
geom_point(size=3) +
# use the simple theme
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(colour="grey60", linetype="dashed"))
# create the ggplot2 data
ggplot(tophit, aes(x=reorder(name, avg), y=avg)) + geom_point(size=3) +
# use the black and white theme
theme_bw() +
# angle axis text
theme(axis.text.x = element_text(angle=60, hjust=1),
# no major y grid
panel.grid.major.y = element_blank(),
# no minor y grid
panel.grid.minor.y = element_blank(),
# dashed major x grids
panel.grid.major.x = element_line(colour="grey60", linetype="dashed"))
# create the basic ggplot data
ggplot(tophit, aes(x=avg, y=name)) +
# create a segment plot, with names at the end of the y axis and a grey line
geom_segment(aes(yend=name), xend=0, colour="grey50") +
# plot the n points and color them
geom_point(size=3, aes(colour=lg)) +
# change the plot color to red and blue
scale_colour_brewer(palette="Set1", limits=c("NL","AL"), guide=FALSE) +
# make the grid simple, black, and white
theme_bw() + theme(panel.grid.major.y = element_blank()) +
# sort the plots by lg
facet_grid(lg ~ ., scales="free_y", space="free_y")