Debian Codegraph @

inst/examples/graphs.brew

raw
1 % Gergely Daróczi
2 % Unified plots
3
4 This document demonstrates the features of unifying plots in `pander`. First, if you do not want to deal with styling each of your images, just activate it: `evalsOptions('graph.unify', TRUE)`.
5
6 *Note*: please install `lattice` and `ggplot2` on your computer prior to trying to run all examples of this document.
7
8 <%
9 ## generating dataset
10 df <- mtcars[, c('hp', 'wt')]
11 df$factor <- sample(c('Foo', 'Bar', 'Foo bar'), size = nrow(df), replace = TRUE)
12 df$factor2 <- sample(c('Foo', 'Bar', 'Foo bar'), size = nrow(df), replace = TRUE)
13 df$time <- 1:nrow(df)
14
15 ## loading packages
16 require(ggplot2, quietly = TRUE)
17 require(lattice, quietly = TRUE)
18
19 ## saving options for restting at the end
20 eO <- evalsOptions()
21 pO <- panderOptions()
22
23 ## setting options
24 evalsOptions('graph.unify', TRUE)
25 evalsOptions('cache', FALSE)
26 %>
27
28 # Options
29
30 There are a bunch of options you might want to check out, these are:
31
32 <%=as.list(names(panderOptions())[grepl('^graph', names(panderOptions()))])%>
33
34 Find more details on [`pander`'s homepage](https://rapporter.github.io/pander/#pander-options).
35
36 # Default options
37
38 Not touching the above ones, let us check out how different plots look like by calling `base` R graphing function, `lattice` or `ggplot2`!
39
40 ## Histogram
41
42 ### Base R plot
43
44 <%=hist(df$hp, main = "Histogram in base R")%>
45
46 ### lattice
47
48 <%=histogram(df$hp, main = "Histogram with lattice")%>
49
50 ### ggplot2
51
52 <%=ggplot(df) + geom_histogram(aes(x = hp), binwidth = 50) + ggtitle("Histogram with ggplot2")%>
53
54 ## Barplot
55
56 ### Base R plot
57
58 <%=barplot(table(df$factor), main = "Barplot in base R")%>
59
60 ### lattice
61
62 <%=barchart(df$factor, main = "Barplot with lattice")%>
63
64 ### ggplot2
65
66 <%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Barplot with ggplot2")%>
67
68 ## Points
69
70 ### Base R plot
71
72 <%=plot(df$hp, df$wt, main = "Points in base R")%>
73
74 ### lattice
75
76 <%=xyplot(df$wt ~ df$hp, main = "Points with lattice")%>
77
78 ### ggplot2
79
80 <%=ggplot(df) + geom_point(aes(x = hp, y = wt)) + ggtitle("Points with ggplot2")%>
81
82 ## Grouped plot
83
84 ### Base R plot
85
86 I have no idea how to do that besides manually adding `points`.
87
88 ### lattice
89
90 <%=xyplot(wt ~ hp, group = factor, data = df, auto.key = TRUE, main = "Grouped plot with lattice")%>
91
92 ### ggplot2
93
94 <%=ggplot(df) + geom_point(aes(x = hp, y = wt, colour = factor)) + ggtitle("Grouped bar with ggplot2")%>
95
96 ## Facets
97
98 ### Base R plot
99
100 I have no idea how to do that besides `par(mfrow=c(foo, bar))`.
101
102 ### lattice
103
104 <%=barchart(table(df$factor,df$factor2), groups = FALSE, layout = c(1,3), main = "Faceted barplot with lattice")%>
105
106 ### ggplot2
107
108 <%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Faceted barplot with ggplot2") + facet_grid(factor2 ~ .)%>
109
110 ## Boxplot
111
112 ### Base R plot
113
114 <%=boxplot(df$hp ~ df$factor, main = "Boxplot in base R")%>
115
116 ### lattice
117
118 <%=bwplot(df$factor ~ df$hp, main = "Boxplot with lattice")%>
119
120 ### ggplot2
121
122 <%=ggplot(df) + geom_boxplot(aes(y = hp, x = factor)) + ggtitle("Boxplot with ggplot2")%>
123
124
125 ## Lines
126
127 ### Base R plot
128
129 <%=plot(df$time, df$hp, type = 'l')%>
130
131 ### lattice
132
133 <%=xyplot(df$hp ~ df$time, type = 'l')%>
134
135 ### ggplot2
136
137 <%=ggplot(df) + geom_line(aes(x = time, y = hp))%>
138
139 # Custom options
140
141 Below you can find some images generated by the exact same commands but with some modified `panderOptions`:
142
143 ```r
144 panderOptions('graph.fontfamily', "Comic Sans MS")
145 panderOptions('graph.fontsize', 18)
146 panderOptions('graph.fontcolor', 'blue')
147 panderOptions('graph.grid.color', 'blue')
148 panderOptions('graph.axis.angle', 3)
149 panderOptions('graph.boxes', T)
150 panderOptions('graph.legend.position', 'top')
151 panderOptions('graph.colors', rainbow(5))
152 panderOptions('graph.grid', FALSE)
153 panderOptions('graph.symbol', 22)
154 ```
155
156 <%
157 panderOptions('graph.fontfamily', "Comic Sans MS")
158 panderOptions('graph.fontsize', 18)
159 panderOptions('graph.fontcolor', 'blue')
160 panderOptions('graph.grid.color', 'blue')
161 panderOptions('graph.axis.angle', 3)
162 panderOptions('graph.boxes', T)
163 panderOptions('graph.legend.position', 'top')
164 panderOptions('graph.colors', rainbow(5))
165 panderOptions('graph.grid', FALSE)
166 panderOptions('graph.symbol', 22)
167 %>
168
169 ## Histogram
170
171 ### Base R plot
172
173 <%=hist(df$hp, main = "Histogram in base R")%>
174
175 ### lattice
176
177 <%=histogram(df$hp, main = "Histogram with lattice")%>
178
179 ### ggplot2
180
181 <%=ggplot(df) + geom_histogram(aes(x = hp), binwidth = 50) + ggtitle("Histogram with ggplot2")%>
182
183 ## Barplot
184
185 ### Base R plot
186
187 <%=barplot(table(df$factor), main = "Barplot in base R")%>
188
189 Yeah, the "Foo bar" label is cropped. We need a custom `mar` option here:
190
191 <%=par(mar=c(6, 4.3, 2.1, 0.1));+barplot(table(df$factor), main = "Barplot in base R")%>
192
193 But wait, we lost the color! Right: unfortunately coloring base R plots is really hackish, `pander` is adding the `col` attribute to the calls. If you start to tweak `par` in a chunk, you should prepare to some unwanted side-effects. Solution:
194
195 <%=par(mar=c(6, 4.3, 2.1, 0.1));+barplot(table(df$factor), main = "Barplot in base R", col = panderOptions('graph.colors')[1])%>
196
197 ### lattice
198
199 <%=barchart(df$factor, main = "Barplot with lattice")%>
200
201 ### ggplot2
202
203 <%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Barplot with ggplot2")%>
204
205 ## Points
206
207 ### Base R plot
208
209 <%=plot(df$hp, df$wt, main = "Points in base R")%>
210
211 ### lattice
212
213 <%=xyplot(df$wt ~ df$hp, main = "Points with lattice")%>
214
215 ### ggplot2
216
217 <%=ggplot(df) + geom_point(aes(x = hp, y = wt)) + ggtitle("Points with ggplot2")%>
218
219 ## Grouped plot
220
221 ### Base R plot
222
223 I have no idea how to do that besides manually adding `points`.
224
225 ### lattice
226
227 <%=xyplot(wt ~ hp, group = factor, data = df, auto.key = TRUE, main = "Grouped plot with lattice")%>
228
229 ### ggplot2
230
231 <%=ggplot(df) + geom_point(aes(x = hp, y = wt, colour = factor)) + ggtitle("Grouped bar with ggplot2")%>
232
233 ## Facets
234
235 ### Base R plot
236
237 I have no idea how to do that besides `par(mfrow=c(foo, bar))`.
238
239 ### lattice
240
241 <%=barchart(table(df$factor,df$factor2), groups = FALSE, layout = c(1,3), main = "Faceted barplot with lattice")%>
242
243 ### ggplot2
244
245 <%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Faceted barplot with ggplot2") + facet_grid(factor2 ~ .)%>
246
247 ## Boxplot
248
249 ### Base R plot
250
251 <%=boxplot(df$hp ~ df$factor, main = "Boxplot in base R")%>
252
253 ### lattice
254
255 <%=bwplot(df$factor ~ df$hp, main = "Boxplot with lattice")%>
256
257 ### ggplot2
258
259 <%=ggplot(df) + geom_boxplot(aes(y = hp, x = factor)) + ggtitle("Boxplot with ggplot2")%>
260
261
262 ## Lines
263
264 ### Base R plot
265
266 <%=plot(df$time, df$hp, type = 'l')%>
267
268 ### lattice
269
270 <%=xyplot(df$hp ~ df$time, type = 'l')%>
271
272 ### ggplot2
273
274 <%=ggplot(df) + geom_line(aes(x = time, y = hp))%>
275
276 <%
277 ## resetting options
278 for (o in names(eO))
279 evalsOptions(o, eO[[o]])
280 for (o in names(pO))
281 panderOptions(o, pO[[o]])
282 %>