Permalink
Fetching contributors…
Cannot retrieve contributors at this time
283 lines (161 sloc) 6.51 KB
% 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]])
%>