knitr 生成的 pdf 从图中剪切边框

knitr 生成的 pdf 从图中剪切边框

我遇到了一个问题,在 RStudio 中正确生成了一个图形,但是编织时其右边距设置不正确。

请考虑以下 MWE

\documentclass[a4papera4paper, twocolumn]{article}
\begin{document}

\begin{figure}
<<dev='tikz', dev.args=list(pointsize=5),dev='pdf',fig.env='figure*'>>=
library(igraph)

g <- graph.full(5, loops=TRUE)

E(g)$weigth <- 200

par(mar = c(0, 0, 0, 16))
plot(g, layout=layout.circle, edge.width=log(E(g)$weigth), edge.label=E(g)$weigth)
@
\end{figure}

\end{document}

产生

在此处输入图片描述

而在RStudio相同的脚本产生

在此处输入图片描述

答案1

看起来此图使用了固定的纵横比,因此您必须遵循该比例以避免剪切,例如,当图太窄时。经过一些测试,在这种情况下,9:7 的宽高比似乎合适。您可以根据此比例调整图形的宽度/高度,例如fig.width = 6, fig.height = 6 * 7/9

<<test, fig.width=9, fig.height=7>>=

igraph 和 knitr

完整代码:

\documentclass[a4paper]{article}
\begin{document}

\begin{figure}
<<test, fig.width=9, fig.height=7>>=
library(igraph)

g <- graph.full(5, loops=TRUE)

E(g)$weigth <- 200

par(mar = c(0, 0, 0, 0))
plot(g, layout=layout.circle, edge.width=log(E(g)$weigth), edge.label=E(g)$weigth)
box()
@
\end{figure}

\end{document}

相关内容