I have different figures across my document with different width. I set the the font size for all ggplot
s globally with theme_set(theme_bw() + theme(text=element_text(family="Palatino", size=20)))
but I want to set the figure width with the knitr
option out.width='
in the preamble of each figure chunk. The problem is this results in different effective font size to be typeset by LaTeX
. The solution of course would be to manually set the font size differently for each chunk.
I just wonder if there's a smarter solution (without using dev=tikz
) so to:
1) Set the font size once and for all plots; 2) Maintain the possibility to set the width size locally for each figure.
Please consider the following
\documentclass{article}
\usepackage{subfig}
\begin{document}
<<set-options, include=FALSE, cache=FALSE>>=
opts_chunk$set(fig.align='center', dev='pdf', cache=FALSE, message=FALSE, echo=FALSE, progress = TRUE, verbose = TRUE, warning=FALSE, error=FALSE)
library(ggplot2)
theme_set(theme_bw() + theme(text=element_text(family="Palatino", size=20)))
@
<<<prepare-data, include=FALSE>>=
data1.df <- data.frame(Plant = c("Plant1", "Plant1", "Plant1", "Plant2", "Plant2",
"Plant2"), Type = c(1, 2, 3, 1, 2, 3), Axis1 = c(0.2, -0.4, 0.8, -0.2, -0.7,
0.1), Axis2 = c(0.5, 0.3, -0.1, -0.3, -0.1, -0.8))
data2.df <- data.frame(Plant = c("Plant1", "Plant1", "Plant1", "Plant2", "Plant2",
"Plant2"), Type = c(1, 2, 3, 1, 2, 3), Axis1 = c(20, 40, 80, 20, 70,
0.1), Axis2 = c(50, 30, 10, 30, 10, 80))
@
<<Fig1, fig.cap='Large', fig.env='figure', message=FALSE, echo=FALSE, warning=FALSE, error=FALSE, out.width='.62\\textwidth'>>=
ggplot(data1.df, aes(x = Axis1, y = Axis2, shape = Plant, color = Type)) + geom_point(size = 5) + annotate("text", x=0.4, y=0.0, label="Label", fontface="italic") + theme(legend.position="none")
@
<<Fig2, fig.cap='Small', subfig.cap=c("First","Second"), fig.env='figure*', message=FALSE, echo=FALSE, warning=FALSE, error=FALSE, out.width='.32\\textwidth'>>=
ggplot(data1.df, aes(x = Axis1, y = Axis2, shape = Plant, color = Type)) + geom_point(size = 5) + annotate("text", x=0.4, y=0.0, label="Label", fontface="italic") + theme(legend.position="none")
ggplot(data2.df, aes(x = Axis1, y = Axis2, shape = Plant, color = Type)) + geom_point(size = 5) + theme(legend.position="none")
@
\end{document}
out.width
for individual code chunks.