Attachment 'ggplot2_tut.R'
Download 1 library(ggplot2)
2 library(plyr)
3
4 ## get an interactive window
5 quartz()
6 ## x11()
7
8 ## lexical decision data
9 data(english, package="languageR")
10
11 ## default scatterplot
12 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
13 main = "Frequency and Reaction Time")
14
15 ## transform the dependent variable
16 qplot(x = WrittenFrequency, y = exp(RTlexdec), data = english)
17
18 ## color for groups
19 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
20 colour=AgeSubject)
21
22 ## color for continuous variable
23 qplot(WrittenFrequency, RTlexdec, data=english,
24 colour=WrittenSpokenFrequencyRatio)
25
26 ## shape for groups
27 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
28 shape=AgeSubject)
29
30 ## colour and shape for groups
31 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
32 colour=AgeSubject,
33 shape=WordCategory)
34
35 ## colour, shape, and size
36 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
37 colour=AgeSubject,
38 shape=WordCategory,
39 size=FamilySize)
40
41 ## panels
42 ## one conditioning variable
43 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
44 colour=AgeSubject,
45 shape=Frication,
46 facets = . ~ WordCategory)
47
48 ## panels with totals shown
49 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
50 colour=AgeSubject,
51 shape=AgeSubject,
52 facets = . ~ WordCategory,
53 margins=TRUE)
54
55 ## two conditioning variables
56 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
57 colour=AgeSubject,
58 shape=Frication,
59 facets = CV ~ WordCategory)
60
61 ## panels with totals shown
62 qplot(x = WrittenFrequency, y = RTlexdec, data = english,
63 colour=AgeSubject,
64 shape=Frication,
65 facets = CV ~ WordCategory,
66 margins=TRUE)
67
68 ## changing type of plot requires changing the "geom"
69 qplot(x = WrittenFrequency, data = english,
70 geom = "density", fill=rev(AgeSubject))
71
72 ## histograms don't have a sensible default for bin size
73 qplot(x = RTlexdec, data = english,
74 geom = "histogram",
75 fill=AgeSubject)
76
77 ## stacked bar charts based on counts are the default
78 qplot(x=WordCategory, data = english,
79 geom = "bar",
80 fill=Voice)
81
82 ## use "fill" positioning to convert them to proportions
83 qplot(x=WordCategory, data = english,
84 geom = "bar",
85 fill=Voice,
86 position="fill")
87
88 ## use "dodge" positioning to unstack them
89 qplot(x=WordCategory, data = english,
90 geom = "bar",
91 fill=Voice,
92 position="dodge")
93
94 ## use stat="identity" if you've already aggregated your data and want
95 ## to plot the results
96 d <- with(english, aggregate(RTlexdec, by=list(Age=AgeSubject, Category=WordCategory), FUN=mean))
97 qplot(x=Age, y=x, data=d,
98 fill=Category,
99 geom="bar",
100 stat="identity",
101 position="dodge")
102
103 ## plots from scratch, without qplot
104
105 ## boxplot
106 bxp <- ggplot(data = english, aes(x=AgeSubject, y=RTlexdec))
107 bxp <- bxp + stat_boxplot(aes(fill=WordCategory))
108 (bxp)
109
110 ## barplot of means
111 bpm <- ggplot(data=english, aes(x=AgeSubject, y=RTlexdec, fill=WordCategory))
112 bpm <- bpm +
113 stat_summary(fun.y=mean, geom="bar", pos="dodge")
114 (bpm)
115
116 ## change the scale on the y axis to zoom in on the relevant region of
117 ## the data
118 ## NB: If you don't want to show the entire range starting
119 ## from 0, you should consider using a dot plot instead of a bar plot
120 bpm <- bpm + scale_y_continuous(limits=c(5,7))
121 (bpm)
122
123 ## bar plot with automatically calculated normal error bars
124 bpe <- ggplot(data=english, aes(x=AgeSubject, y=RTlexdec, fill=WordCategory))
125 bpe <- bpe + stat_summary(fun.y="mean", geom="bar", pos="dodge")
126 bpe <- bpe + stat_summary(fun.data="mean_cl_normal", geom="errorbar", width=0.2)
127 (bpe)
128
129 ## dot plot of means
130 dpm <- ggplot(data=english, aes(x=AgeSubject, y=RTlexdec, colour=WordCategory))
131 dpm <- dpm + stat_summary(fun.y="mean", geom="point")
132 (dpm)
133
134 ## dot plot of means with error bars of 2*sd
135 dpe <- ggplot(data=english,
136 aes(x=AgeSubject, y=RTlexdec, colour=WordCategory))
137 dpe <- dpe + stat_summary(fun.data="mean_sdl", geom="pointrange")
138 (dpe)
139
140 ## horizontal dotplot, often used to display regression parameters
141 dpe <- dpe + coord_flip()
142 (dpe)
143
144 ## xyplot with grouping by color
145 xy.tr <- ggplot(data=english, aes(x=WrittenFrequency, y=RTlexdec, colour=AgeSubject))
146 xy.tr <- xy.tr + geom_point()
147 ## now add a specific color scale where we define an alpha level
148 xy.tr <- xy.tr + scale_colour_hue(alpha=1/3)
149 (xy.tr)
150
151 ## add a smoother to our scatterplot
152 xy.tr <- xy.tr + stat_smooth()
153 (xy.tr)
154
155 ## or we can specify that the smooth be based on a linear model
156 qplot(WrittenFrequency, RTlexdec, data=english, colour=AgeSubject) +
157 scale_colour_hue(alpha=1/3) +
158 stat_smooth(method=lm)
159
160 ## and we can add a robust linear smooth if we want
161 library(MASS)
162 last_plot() + stat_smooth(method=rlm)
163
164 ## apply the black and white theme to our plot
165 last_plot() + theme_bw()
166
167 ## and change font sizes. See page 123 and 124 of the ggplot2 book.
168 last_plot() + opts(title = "Frequency Effects",
169 plot.title=theme_text(size=24))
170
171
172 ## finally, let's make a hexbinplot
173 hbp <- ggplot(english, aes(x=WrittenFrequency, y=RTlexdec)) +
174 stat_binhex() +
175 stat_smooth(method=rlm, colour=I("orange"), size=1.5) +
176 opts(panel.background=theme_blank())
177 hbp
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.