10

I have different figures across my document with different width. I set the the font size for all ggplots 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}
  • Perhaps there is no smart solution. I always set out.width for individual code chunks. – Yihui Xie Nov 25 '13 at 4:23
1

You could supply the length that you want out.width to be relative to as a constant in R and call that in your plots. You can find the length of \textwidth by complining your .tex file with \the\textwidth, which for the article class with [letter] is 345 points (about 4.7917 inches). Then you supply fig.width with a number times your constant.

However, doing this will also require that you set fig.height since the aspect ratio is not constrained by default.

\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))
@

<<sizes, include=FALSE>>=
textwidth <- 4.7917 # Default \textwidth with \usepackage[letter]{article}
@

<<Fig1, fig.cap='Large', fig.env='figure', message=FALSE, echo=FALSE, warning=FALSE, error=FALSE, fig.width=.62*textwidth, fig.height=.52*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, fig.width=.32*textwidth, fig.height=.27*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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.