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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
% Gergely Daróczi
% Unified plots
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)`.
*Note*: please install `lattice` and `ggplot2` on your computer prior to trying to run all examples of this document.
<%
## generating dataset
df <- mtcars[, c('hp', 'wt')]
df$factor <- sample(c('Foo', 'Bar', 'Foo bar'), size = nrow(df), replace = TRUE)
df$factor2 <- sample(c('Foo', 'Bar', 'Foo bar'), size = nrow(df), replace = TRUE)
df$time <- 1:nrow(df)
## loading packages
require(ggplot2, quietly = TRUE)
require(lattice, quietly = TRUE)
## saving options for restting at the end
eO <- evalsOptions()
pO <- panderOptions()
## setting options
evalsOptions('graph.unify', TRUE)
evalsOptions('cache', FALSE)
%>
# Options
There are a bunch of options you might want to check out, these are:
<%=as.list(names(panderOptions())[grepl('^graph', names(panderOptions()))])%>
Find more details on [`pander`'s homepage](http://rapporter.github.com/pander/#pander-options).
# Default options
Not touching the above ones, let us check out how different plots look like by calling `base` R graphing function, `lattice` or `ggplot2`!
## Histogram
### Base R plot
<%=hist(df$hp, main = "Histogram in base R")%>
### lattice
<%=histogram(df$hp, main = "Histogram with lattice")%>
### ggplot2
<%=ggplot(df) + geom_histogram(aes(x = hp), binwidth = 50) + ggtitle("Histogram with ggplot2")%>
## Barplot
### Base R plot
<%=barplot(table(df$factor), main = "Barplot in base R")%>
### lattice
<%=barchart(df$factor, main = "Barplot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Barplot with ggplot2")%>
## Points
### Base R plot
<%=plot(df$hp, df$wt, main = "Points in base R")%>
### lattice
<%=xyplot(df$wt ~ df$hp, main = "Points with lattice")%>
### ggplot2
<%=ggplot(df) + geom_point(aes(x = hp, y = wt)) + ggtitle("Points with ggplot2")%>
## Grouped plot
### Base R plot
I have no idea how to do that besides manually adding `points`.
### lattice
<%=xyplot(wt ~ hp, group = factor, data = df, auto.key = TRUE, main = "Grouped plot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_point(aes(x = hp, y = wt, colour = factor)) + ggtitle("Grouped bar with ggplot2")%>
## Facets
### Base R plot
I have no idea how to do that besides `par(mfrow=c(foo, bar))`.
### lattice
<%=barchart(table(df$factor,df$factor2), groups = FALSE, layout = c(1,3), main = "Faceted barplot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Faceted barplot with ggplot2") + facet_grid(factor2 ~ .)%>
## Boxplot
### Base R plot
<%=boxplot(df$hp ~ df$factor, main = "Boxplot in base R")%>
### lattice
<%=bwplot(df$factor ~ df$hp, main = "Boxplot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_boxplot(aes(y = hp, x = factor)) + ggtitle("Boxplot with ggplot2")%>
## Lines
### Base R plot
<%=plot(df$time, df$hp, type = 'l')%>
### lattice
<%=xyplot(df$hp ~ df$time, type = 'l')%>
### ggplot2
<%=ggplot(df) + geom_line(aes(x = time, y = hp))%>
# Custom options
Below you can find some images generated by the exact same commands but with some modified `panderOptions`:
```r
panderOptions('graph.fontfamily', "Comic Sans MS")
panderOptions('graph.fontsize', 18)
panderOptions('graph.fontcolor', 'blue')
panderOptions('graph.grid.color', 'blue')
panderOptions('graph.axis.angle', 3)
panderOptions('graph.boxes', T)
panderOptions('graph.legend.position', 'top')
panderOptions('graph.colors', rainbow(5))
panderOptions('graph.grid', FALSE)
panderOptions('graph.symbol', 22)
```
<%
panderOptions('graph.fontfamily', "Comic Sans MS")
panderOptions('graph.fontsize', 18)
panderOptions('graph.fontcolor', 'blue')
panderOptions('graph.grid.color', 'blue')
panderOptions('graph.axis.angle', 3)
panderOptions('graph.boxes', T)
panderOptions('graph.legend.position', 'top')
panderOptions('graph.colors', rainbow(5))
panderOptions('graph.grid', FALSE)
panderOptions('graph.symbol', 22)
%>
## Histogram
### Base R plot
<%=hist(df$hp, main = "Histogram in base R")%>
### lattice
<%=histogram(df$hp, main = "Histogram with lattice")%>
### ggplot2
<%=ggplot(df) + geom_histogram(aes(x = hp), binwidth = 50) + ggtitle("Histogram with ggplot2")%>
## Barplot
### Base R plot
<%=barplot(table(df$factor), main = "Barplot in base R")%>
Yeah, the "Foo bar" label is cropped. We need a custom `mar` option here:
<%=par(mar=c(6, 4.3, 2.1, 0.1));+barplot(table(df$factor), main = "Barplot in base R")%>
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:
<%=par(mar=c(6, 4.3, 2.1, 0.1));+barplot(table(df$factor), main = "Barplot in base R", col = panderOptions('graph.colors')[1])%>
### lattice
<%=barchart(df$factor, main = "Barplot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Barplot with ggplot2")%>
## Points
### Base R plot
<%=plot(df$hp, df$wt, main = "Points in base R")%>
### lattice
<%=xyplot(df$wt ~ df$hp, main = "Points with lattice")%>
### ggplot2
<%=ggplot(df) + geom_point(aes(x = hp, y = wt)) + ggtitle("Points with ggplot2")%>
## Grouped plot
### Base R plot
I have no idea how to do that besides manually adding `points`.
### lattice
<%=xyplot(wt ~ hp, group = factor, data = df, auto.key = TRUE, main = "Grouped plot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_point(aes(x = hp, y = wt, colour = factor)) + ggtitle("Grouped bar with ggplot2")%>
## Facets
### Base R plot
I have no idea how to do that besides `par(mfrow=c(foo, bar))`.
### lattice
<%=barchart(table(df$factor,df$factor2), groups = FALSE, layout = c(1,3), main = "Faceted barplot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_bar(aes(x = factor)) + ggtitle("Faceted barplot with ggplot2") + facet_grid(factor2 ~ .)%>
## Boxplot
### Base R plot
<%=boxplot(df$hp ~ df$factor, main = "Boxplot in base R")%>
### lattice
<%=bwplot(df$factor ~ df$hp, main = "Boxplot with lattice")%>
### ggplot2
<%=ggplot(df) + geom_boxplot(aes(y = hp, x = factor)) + ggtitle("Boxplot with ggplot2")%>
## Lines
### Base R plot
<%=plot(df$time, df$hp, type = 'l')%>
### lattice
<%=xyplot(df$hp ~ df$time, type = 'l')%>
### ggplot2
<%=ggplot(df) + geom_line(aes(x = time, y = hp))%>
<%
## resetting options
for (o in names(eO))
evalsOptions(o, eO[[o]])
for (o in names(pO))
panderOptions(o, pO[[o]])
%>
|