Created
April 14, 2019 00:33
-
-
Save baptiste/515261aadd8f4c83b89cbf83e9ac0331 to your computer and use it in GitHub Desktop.
fixed width plot panel, rest hanging in margins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Title" | |
header-includes: | |
- \usepackage{lipsum} | |
output: | |
pdf_document: | |
fig_caption: yes | |
fig_crop: no | |
keep_tex: yes | |
geometry: width=5in | |
--- | |
```{r setup, include=FALSE} | |
library(ggplot2) | |
library(dplyr) | |
library(grid) | |
library(knitr) | |
general_fig_width <- 5 | |
``` | |
```{r plot, fig.show=FALSE, echo=FALSE} | |
p <- diamonds %>% | |
sample_frac(0.3) %>% | |
ggplot(aes(x = carat, y = price, color = price)) + | |
geom_point() + | |
theme_grey() + | |
theme(plot.margin = unit(c(0,0,0,0), "pt")) | |
g <- ggplotGrob(p) | |
panel_id <- g$layout$l[grep('panel', g$layout$name)] | |
g$widths[panel_id] <- unit(general_fig_width, "in") | |
fig_width <- convertWidth(sum(g$widths), "in", valueOnly = TRUE) | |
left_width <- convertWidth(sum(g$widths[seq_len(panel_id-1)]), "in", valueOnly = TRUE) | |
ggsave('plot-tmp.pdf', width=fig_width, height=4) | |
``` | |
\lipsum[1] | |
\begin{figure}[!hb] | |
\hspace{`r -left_width`in}\includegraphics[width=`r fig_width`in]{plot-tmp} | |
\end{figure} | |
\lipsum[2] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment