首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >独立于行小面数控制ggplot2小面高度

独立于行小面数控制ggplot2小面高度
EN

Stack Overflow用户
提问于 2013-09-08 11:53:21
回答 1查看 5K关注 0票数 10

问题

ggplot2/针织品强制绘图(在LaTeX中)具有默认的高宽比。两个面数不同的样地导致面高不同的样地。这不太好。

我正在寻找一种解决方案,它可以独立于行面的数量来控制ggplot2面的高度。

编辑的9月。9:解决方案应该在一个块内工作,因为这两幅图都在一个块中(如示例代码中所示)。因此,调整针织机的数字相关的块选项是不可行的,因为块选项同样适用于两个地块。

解决办法-但有问题

将每一幅图放入一个gridExtra框中,并用小面的数量来缩放它的高度,结果是小面高度不再不同:更好但不完美。

  • 图解显示在页面上,有太多的空白处,即使下面还有剩余的文字,情节也会移动得更近。
  • 似乎被这种做法所操纵的阴谋被砍掉了。

是否有一种更智能、无问题的解决方案,可以独立于行面的数量来控制ggplot2面的高度?

代码语言:javascript
复制
\documentclass[a4paper]{article}
\usepackage[margin=1in]{geometry}

\begin{document}

<<setup, results='asis', message=FALSE, echo=FALSE>>=
require(ggplot2)
require(gridExtra)

### Generate two data frames

# Data frame with 2 classes
db.small = data.frame (
  class = as.factor(c(rep("A", 12), rep("B", 12))),
  month = as.factor(rep(1:12, 2)),
  value = runif(2*12, 0, 100)
  )

# Data frame with 5 classes
db.large = data.frame (
  class = as.factor(c(rep("A", 12), rep("B", 12), rep("C", 12), rep("C", 12), rep("D", 12))),
  month = as.factor(rep(1:12, 5)),
  value = runif(5*12, 0, 100)
)

# Generate plots
plot1 = ggplot(db.small, aes(month, value)) + geom_bar(stat="identity") + facet_grid(class ~ .) + ylim(c(0, 100))
plot2 = ggplot(db.large, aes(month, value)) + geom_bar(stat="identity") + facet_grid(class ~ .) + ylim(c(0, 100))
@

\section{Before: Native plots without modification}
Ggplot2/knitr forces both plots to have the default aspect ratio. As both plots have different number of facets the result is that the facet heights are different. This is not nice.

<<before, results='asis', message=FALSE, echo=FALSE, out.width="0.6\\linewidth">>=
print(plot1)
print(plot2)
@

\pagebreak
\section{After: Plots with modification}
Putting each plot into a gridExtra box and scaling it's height with the number of facets, results in facet heights which are no longer different: nicer but not perfect, see problems below.

<<After, results='asis', message=FALSE, echo=FALSE, out.width="0.6\\linewidth">>=
# Calculate number of facets for the two plots
db.small.numberfacets = length(unique(db.small$class))
db.large.numberfacets = length(unique(db.large$class))

# Define heights for modification of plots
facet.height = 4
other.height = 2

plot1 = plot1 + theme( plot.margin = unit(c(0, 0, 0, 0), "cm"))
plot2 = plot2 + theme( plot.margin = unit(c(0, 0, 0, 0), "cm"))

# Put plots inside a gridExtra box and change height plots
grid.arrange(arrangeGrob(plot1, heights=unit(db.small.numberfacets*facet.height+other.height, "cm")), nrow=1)
grid.arrange(arrangeGrob(plot2, heights=unit(db.large.numberfacets*facet.height+other.height, "cm")), nrow=1)
@


2 Problems:
\begin{itemize}
\item Plots appear on the page with too much margin, even there's remaining text below so plots shall move more closer.
\item Seems the second plot is cropped, see the x-axis label.
\end{itemize}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

\end{document}

pdf输出:

EN

回答 1

Stack Overflow用户

发布于 2013-09-08 18:47:33

你知道块选项能接受任意的R表达式吗?实例40展示了如何动态计算fig.widthfig.height

对于您的情况,您可以根据因子变量中的级别数为fig.height分配一个值。例如,

代码语言:javascript
复制
\documentclass{article}

\begin{document}
<<setup>>=
library(ggplot2)
calc_height = function(f) length(levels(f))
p = ggplot(diamonds, aes(color)) + geom_bar()
@

<<test-a, fig.width=7, fig.height=calc_height(diamonds$cut)>>==
p + facet_grid(cut ~ .)
@

<<test-b, fig.width=7, fig.height=calc_height(diamonds$clarity)>>==
p + facet_grid(clarity ~ .)
@

\end{document}

输出:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18683370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档