Skip to content

Instantly share code, notes, and snippets.

@HughParsonage
Created March 13, 2016 03:28
Show Gist options
  • Save HughParsonage/46f5998536a874eb50b5 to your computer and use it in GitHub Desktop.
Save HughParsonage/46f5998536a874eb50b5 to your computer and use it in GitHub Desktop.
Including charts in knitr where plot background has the same margin as figure width
\documentclass[twocolumn, landscape]{article}
\usepackage{geometry}
\usepackage{float, placeins}
\addtolength{\columnsep}{25pt}
\usepackage{lipsum}
\title{Making charts in \LaTeX{} + \texttt{knitr} where $x = 0$ is on the left margin.}
\begin{document}
\maketitle
<<knitrOpts, echo=FALSE>>=
knitr::opts_chunk$set(echo = FALSE,
warning = FALSE,
message = FALSE,
fig.width = general_fig_width <- 7,
out.width = "7in",
fig.height = 4,
out.height = "4in",
results = "hide",
fig.show = "hide")
@
<<iris-plot, echo = FALSE>>=
library(ggplot2)
library(grid)
ggplot(diamonds) +
geom_point(aes(x = carat, y = price))
# For width identification
# grid.ls(view=TRUE,grob=FALSE)
# current.vpTree()
seekViewport('panel.3-4-3-4')
a <- convertWidth(unit(1,'npc'), 'inch', TRUE)
width_factor <- general_fig_width / a
@
\lipsum
\begin{figure}[t]
\makebox[\columnwidth][r]{\includegraphics[width = \Sexpr{width_factor}\columnwidth]{figure/iris-plot-1}}
\caption{\Sexpr{paste0("The width of the panel is ", round(a, 2), " and the ratio used is ", round(width_factor,2))}}
\end{figure}
\lipsum
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment