4

I'm trying to include a plotly chart into a Latex document with knitr. Since knitr includes the webshot package this works well. But if I want to resize my figure for the latex output, the figure environment gets bigger but the plotly chart is not scalled to the manually set figure width and height.

Resulting pdf with knitr

Specifing the webshot options like recommend here, did not work neither. Scaling a ggplot chart works well, but how can I get the same results for the plotly chart?

\documentclass{article}
\usepackage{cleveref}

<<setup, echo=FALSE, message = FALSE, warning = FALSE>>=
library(ggplot2)
library(plotly)
@

%Preamble
\title{GGplot vs. Plotly }
\author{authors}
\date{2016}

\begin{document}

\maketitle

\section{Comparison ggplot with plotly}

This document illustrate the difference between ggplot and plotly. The chart in (\Cref{fig:ggplot}) is well placed in the document, but the chart in (\Cref{fig:plotly}) is neither scalled to the figure width, nor positioned next to the label.

<<ggplot, fig.lp="fig:", fig.cap = 'This is rendered with ggplot()', echo=FALSE, fig.width = 10, fig.height = 6>>=
ggplot(midwest, aes(x = state, y = percollege, colour = state)) + geom_boxplot()
@

<<plotly, fig.lp="fig:", fig.cap = 'This is rendered with plotly()', echo=FALSE, fig.width = 10, fig.height = 6, screenshot.opts = list(delay = 2, cliprect = 'viewport')>>=
plot_ly(midwest, x = ~state, y = ~percollege, color = ~state, type = "box")
@

\end{document}

I saw that the graphic file figure/plotly-1.pdf (generated by knitr/webshot, and then loaded into latex) has two pages and the plotly object is somewhere in the upper left corner. I guess the margins included in the webshot figure are in fact the problem.

Resulting webshot pdf in figures/plotly-1.pdf


R 3.2.3 knitr 1.14 ggplot2 2.1.0 plotly 4.5.2

Nino
  • 366
  • 2
  • 12
  • With the development versions of ggplot2 and plotly this error is not reproducible. – J_F Nov 03 '16 at 12:41
  • @J_F Thanks. I installed the latest development version of plotly with devtools::install_github("ropensci/plotly") following your comment. But I still have the same problem - the plotly chart is not scalled to the figure dimension. I added the resulting pdf extract in my question. – Nino Nov 03 '16 at 12:52
  • For me, it looks like [this](https://i.stack.imgur.com/EyOXA.jpg). – J_F Nov 03 '16 at 13:07
  • @J_F acctually it looks like my version. As you can see the plotly chart is not filling the whole figure space, but rather stays smaller in the upper left corner (with huge white margings to the figure caption), when compared to the ggplot2. – Nino Nov 03 '16 at 13:52
  • @Nino, have you found a solution? – amberv Jun 08 '17 at 17:12
  • No, this has to be related to how webshot works with plotly vector graphics. If I due the same thing with a networkd3 graphic everything works fine. Possible workaround is to add `dev = 'png'` to the R-chunk, which gives you proper dimensions but losing the vector graphic. So no good solution yet. Maybe the people of the webshot or plotly package could help. – Nino Jun 15 '17 at 12:23
  • I'm experiencing the same problem on OSX with plotly_4.7.1. – Tim Aug 03 '17 at 12:08

0 Answers0