【发布时间】:2016-02-24 12:02:22
【问题描述】:
我刚刚发现了这个工具 knitr。我希望它使用的第一件事是生成简单的报告附录,其中包含一个带有内容描述的页面和一系列带有绘图的页面,每页一个绘图,循环生成的绘图。 但是,我遇到了单个绘图大小的问题 - 它没有填满页面上的整个可用空间。我尝试了 fig.width、fig.height 和 ang 的不同设置,谷歌搜索了一下,但到目前为止没有任何效果。 这是它现在的样子:
红色矩形是绘图所需大小的近似值。 代码如下:
\documentclass{article}
\begin{document}
<< echo =FALSE >>=
suppressMessages(suppressWarnings(library("ggplot2")))
suppressMessages(suppressWarnings(library("RColorBrewer")))
colours <- brewer.pal(11, "RdYlGn")[3:9]
@
<< echo=FALSE, fig.width = 8.3, fig.height = 11.7, fig.align = 'center' >>=
name.percentage <- data.frame(name = paste0(LETTERS[1:30], letters[1:30], sample(LETTERS[1:30], size = 30, replace = TRUE )),
percentage = 0.85 + runif(30, 0, 0.15))
name.percentage <- rbind(
transform(name.percentage, type = 1, fill = cut(percentage, breaks = c(-Inf,(1:6 * 3 + 81)/100, Inf), right = T, labels = colours)),
transform(name.percentage, percentage = 1 - percentage, type = 2, fill = "#EEEEEE")
)
plot <- ggplot(data = name.percentage,
aes( x = name, y = percentage, fill = fill)) +
geom_bar(stat = "identity", position = "stack", width = 0.75) +
scale_fill_identity(guide = "none") +
labs(x = NULL, y = NULL) +
scale_y_continuous(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0)) +
coord_flip() +
theme_classic() +
theme(axis.ticks.y = element_blank(),
axis.text.y = element_text(size = 11, colour = "black" ),
axis.text.x = element_text(size = 11, colour = "black" ),
axis.line = element_blank(),
plot.margin = unit(c(0,5,0,0),"mm"),
aspect.ratio = 1.45)
print(plot)
@
\end{document}
任何建议将不胜感激!
【问题讨论】:
-
在这种情况下,您只是低估了 LaTeX 中的默认边距。在您的文档中添加一些文本,您将看到该图形已经与正常文本宽度一样宽。 LaTeX 包
geometry是改变这一点的便捷方式。一般来说,看看optionout.width并了解它与fig.width有何不同。此外,请注意图形周围的白色边距也会增加图形宽度。hook_pdfcrop可以帮助你摆脱它们。 -
哇,我的 LaTeX 技能确实很生疏……我添加了一些文字,你说得对!
-
将您的解决方案添加为答案,以便人们知道此问题已解决。