Scaling plotly figures in knitr Latex document

流过昼夜 提交于 2019-12-07 08:08:58

问题


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.

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.


R 3.2.3 knitr 1.14 ggplot2 2.1.0 plotly 4.5.2

来源:https://stackoverflow.com/questions/40401200/scaling-plotly-figures-in-knitr-latex-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!