Skip to content

Instantly share code, notes, and snippets.

@yihui
Created December 23, 2013 22:15
Show Gist options
  • Save yihui/8105762 to your computer and use it in GitHub Desktop.
Save yihui/8105762 to your computer and use it in GitHub Desktop.
The Rnw document that confused several people in R-help.
\documentclass{article}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
library(ggplot2)
@
\title{Knitr and ggplot2}
\author{Daniel Haugstvedt}
\maketitle
There are four plots in this article. Figure \ref{fig:plot-figHeight} uses
the argument fig.height=2.5 while Figures \ref{fig:plot-figWidth}
used both fig.height=2.5 and fig.width=3. The later option makes the font
too big.
An alternative approach is used in Figures \ref{fig:plot-figOutWidthBig}
and
\ref{fig:plot-figOutWidthSmall}. There the argument out.width is set to
12 and 8 cm respectively. This stops the problem of excessively large fonts
for figures with smaller width, but there is still no consistency
across plots in terms of font size.
<<plot-figHeight, fig.height=2.5, fig.cap="Density plot with no fig.width argument", fig.pos='ht'>>=
df = data.frame(x = rnorm(100), y = 1:100)
ggplot(df, aes(x = x)) +
geom_histogram(aes(y = ..density..),
binwidth = 1, colour = "black", fill = "white") +
xlab("Improvement, %") +
ylab("Density") +
theme_classic()
@
<<plot-figWidth, echo=FALSE, fig.height=2.5, fig.width = 3, fig.cap="Density plot with fig.width=3", fig.pos='ht'>>=
ggplot(df, aes(x = x)) +
geom_histogram(aes(y = ..density..),
binwidth = 1, colour = "black", fill = "white") +
xlab("Improvement, %") +
ylab("Density") +
theme_classic()
@
<<plot-figOutWidthBig, echo=FALSE, fig.height=2.5, out.width = "12cm", fig.cap="Density plot with out.width=12cm", fig.pos='ht'>>=
ggplot(df, aes(x = x)) +
geom_histogram(aes(y = ..density..),
binwidth = 1, colour = "black", fill = "white") +
xlab("Improvement, %") +
ylab("Density") +
theme_classic()
@
<<plot-figOutWidthSmall, echo=FALSE, fig.height=2.5, out.width = "8cm", fig.cap="Density plot with out.width=8cm", fig.pos='ht'>>=
ggplot(df, aes(x = x)) +
geom_histogram(aes(y = ..density..),
binwidth = 1, colour = "black", fill = "white") +
xlab("Improvement, %") +
ylab("Density") +
theme_classic()
@
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment