9

問題

ggplot2 / Knitrは、(LaTeX の) プロットをデフォルトの縦横比に強制します。ファセットの数が異なる 2 つのプロットは、ファセットの高さが異なるプロットになります。これは良くありません。

問題の例

行ファセットの数とは別に、ggplot2 ファセットの高さを制御するソリューションを探していました。

9 月 9 日に編集: 両方のプロットが 1 つのチャンク内にあるため、ソリューションは 1 つのチャンク内で機能するはずです (コード例のように)。したがって、chunk オプションは両方のプロットに等しく適用されるため、knitr の Figure 関連のチャンク オプションを微調整することは実行できません。

解決策 - しかし問題がある

各プロットをgridExtraボックスに入れ、その高さをファセットの数でスケーリングすると、ファセットの高さが変わらなくなります。より良くなりますが、完全ではありません。

  • 下にテキストが残っている場合でも、プロットがページに表示される余白が多すぎるため、プロットがより近くに移動します。
  • これによって操作されたプロットがトリミングされているようです。

行ファセットの数とは無関係に ggplot2 ファセットの高さを制御する、よりスマートで問題のないソリューションはありますか?

\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 出力: ページ1 ページ2

4

1 に答える 1