2017-10-30 31 views
3

私は、RMarkdownドキュメントのいくつかのggplotでパネルの幅を調整したいと思います。伝説を上か下に動かすことができますが、それは理想的ではありません。右側の凡例でパネルの幅を指定する方法はありますか?RMarkdownドキュメントのggplotパネルの幅を修正してください

--- 
output: html_document 
--- 

```{r} 
library(ggplot2) 
d <- data.frame(x = c("a", "b"), fill = c("short", "labels")) 
ggplot(d, aes(x = x, fill = fill)) + 
    geom_bar() 
``` 

```{r} 
d$fill <- c("Now the labels are longer", "which compresses the plotting area") 
ggplot(d, aes(x = x, fill = fill)) + 
    geom_bar() 
``` 

が生成されます html output

答えて

1

別のオプションは、凡例を抽出しgridExtraパッケージからgrid.arrangeを使用して別グロブ(グラフィカル・オブジェクト)としてレイアウトすることであろう。こうすることで、両方のプロットのgrid.arrangeの引数が同じである限り、両方のプロットのプロットエリアに同じパネルサイズが得られます。

--- 
output: html_document 
--- 

```{r, include=FALSE} 
library(ggplot2) 
library(gridExtra) 
knitr::opts_chunk$set(echo=FALSE) 

# Extract the plot legend as a separate grob 
# http://stackoverflow.com/questions/12539348/ggplot-separate-legend-and-plot 
g_legend<-function(a.gplot){ 
    tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
    legend <- tmp$grobs[[leg]] 
    legend 
} 

thm = theme(legend.justification="left") 

widths = c(8,5) 
``` 

```{r} 
d <- data.frame(x = c("a", "b"), fill = c("short", "labels")) 
p1 = ggplot(d, aes(x = x, fill = fill)) + 
    geom_bar() + 
    thm 

leg = g_legend(p1) 

grid.arrange(p1 + guides(fill=FALSE), leg, widths=widths) 
``` 

```{r} 
d$fill <- c("Now the labels are longer", "which compresses the plotting area") 
p1 = ggplot(d, aes(x = x, fill = fill)) + 
    geom_bar() + 
    thm 

leg = g_legend(p1) 

grid.arrange(p1 + guides(fill=FALSE), leg, widths=widths) 
``` 

enter image description here

+0

ありがとう、これは素晴らしい作品です。凡例を持たないプロットでは、 'grid.arrange(p、nullGrob()、widths = widths)'が必要です。 – mlevy

+0

また、少なくともhtml_notebookの出力では、 'grid.arrange'行は' g_legend'呼び出しとは異なるコード塊になければならず、空のキャンバスが現れます。 – mlevy

+1

凡例がない場合、実際には 'nullGrob()'は必要ありません。これを行うことができます: 'grid.arrange(p、widths = widths)'。 'widths'引数は、2番目の明示的grobがなくても同じ空間レイアウトになります。 – eipi10

2

egg::set_panel_sizeは役立つかもしれません。これはちょっと不便ですが、それに応じてチャンクのfig.widthを調整する必要があるため、以前のチャンクでプロットを作成する必要があります。

--- 
output: html_document 
--- 

```{r} 
library(ggplot2) 
library(egg) 
library(grid) 
d <- data.frame(x = c("a", "b"), fill = c("short", "labels")) 
p1 <- ggplot(d, aes(x = x, fill = fill)) + geom_bar() 
d$fill <- c("Now the labels are longer", "which compresses the plotting area") 
p2 <- ggplot(d, aes(x = x, fill = fill)) + 
    geom_bar() 
g1 <- egg::set_panel_size(p1, width=unit(4,"in")) 
g2 <- egg::set_panel_size(p2, width=unit(4,"in")) 
w1 <- convertWidth(sum(g1$widths), "in", TRUE) 
w2 <- convertWidth(sum(g2$widths), "in", TRUE) 
``` 

```{r, fig.width=w1} 
p1 
``` 

Some text. 

```{r,fig.width=w2} 
p2 
``` 

enter image description here

+0

これは素晴らしいです。残念ながら、出力がhtml_notebookの場合、変数参照される 'fig.width'は尊重されていないようです。 – mlevy

0

必要に応じて、あなたはプロットを手配することができますいくつかのパッケージがあります。 egg:ggarrangeの機能を見てください。

--- 
output: html_document 
--- 

```{r} 
library(ggplot2) 
library(grid) 
library(gridExtra) 
library(egg) 


d <- data.frame(x = c("a", "b"), fill = c("short", "labels")) 
p1 <- ggplot(d, aes(x = x, fill = fill)) + 
    geom_bar() 
``` 

```{r} 
d$fill <- c("Now the labels are longer", "which compresses the plotting area") 
p2 <- ggplot(d, aes(x = x, fill = fill)) + 
    geom_bar() 
``` 

```{r} 
grid.draw(ggarrange(p1, p2, heights = c(1, 1))) 
``` 

ggarrange example

関連する問題