2017-05-31 4 views
1

ここでは例として水平バープロットを使用していますが、それ自体は問題ではありません。一般的な問題は、特に、barplotsが使用する領域を削減するためにgmarkplotsの周りのRmarkdown切り口空白

coord_fixed

+(比= ...)

を使用した後rmarkdownでggplotsの周りに作成されたホワイトスペースをどのように扱うかであります私は水平バープロットを好んでいますが、coord_fixed(ratio = ...)を使ってバーの幅と長さの間の合理的な比を持っています。

プロット自体は、私が望む(以下の最初の例を参照)ように見えますが、その周りに空白がたくさんあります。これは、knitr-headerでfig.height = ...を使って削除しようとしました。次の例が示すように、これはうまくいきませんでした。

library(ggplot2) 
library(ggstance) 
library(dplyr) 

X <- data.frame(X = sample(c("A", "B"), 30, replace = TRUE)) 
X <- X %>% group_by(X) %>% summarise(n = n()) 
ggplot(X, aes(y = X, x = n))+ 
    geom_barh(stat = "identity")+ 
    coord_fixed(ratio = 1)+ 
    ggtitle("blub") 

x = 3を使用したrmarkdown内部でこれを使用します。 x = 1; X = 0.5:

```{r,fig.height = x} 

# see code above 
``` 

これはもたらす:

fig.height = 3

enter image description here

fig.height = 1

enter image description here

fig.height = 0.5

私はそれを行うことを望んだ何

enter image description here

  • 作物白い余白
  • プロット(プロッ​​ト+(軸)のタイトル)を維持しながら、エリア自体に一定

プロット領域が狭くなり、タイトルがプロットの内側に移動するので、後者は明らかに起こりません。

解決策がggplotまたはknitrの内部にあるかどうかわかりません。さらに、理想的なソリューションは、ループ内で使用するために簡単に自動化する必要があります(さまざまな要因にソリューションを適合させる)。

+0

わからない(あなたはfig.heigthを持っている)ことがかもしれないので、あなたが望むように働かないでください。 – sconfluentus

+0

@sconfluentus気づいてくれてありがとう、本当のコードをチェックしてくれてありがとう、私はそれを正しく綴っていると言うことができます。 – Dries

答えて

2

あなたはfig.widthfig.heightチャンクオプションにパラメータを渡すことができます。あなたはそのチャンクを評価する前にそれらを計算する必要があります。

--- 
output: html_document 
--- 

```{r, message=FALSE} 
library(ggplot2) 
library(ggstance) 
library(dplyr) 

X <- data.frame(X = sample(c("A", "B"), 30, replace = TRUE)) 
X <- X %>% group_by(X) %>% summarise(n = n()) 

hei <- length(X$X) + 1 
len <- max(X$n) + 2 

``` 

Text before the plot 
```{r, fig.height=hei, fig.width=len, echo=FALSE} 

ggplot(X, aes(y = X, x = n))+ 
    geom_barh(stat = "identity")+ 
    coord_fixed(ratio = 1)+ 
    ggtitle("blub") + 
    theme(plot.background = element_rect(fill = 'red')) 

``` 

Text after the plot 

```{r, message=FALSE} 

X <- data.frame(X = sample(c("A", "B", "C"), 30, replace = TRUE)) 
X <- X %>% group_by(X) %>% summarise(n = n()) 

hei <- length(X$X) + 1 
len <- max(X$n) + 2 

``` 
Another plot 
```{r, fig.height=hei, fig.width=len, echo=FALSE} 

ggplot(X, aes(y = X, x = n))+ 
    geom_barh(stat = "identity")+ 
    coord_fixed(ratio = 1)+ 
    ggtitle("blub") + 
    theme(plot.background = element_rect(fill = 'red')) 

``` 

結果:これはあなたの本当のコードですが、それはfig.heightある場合 enter image description here

0

私は、あなたが高さと幅の間に良い比率を見つける必要があると思います。しかし、これは時には簡単ではありません。
以前は、PNG画像の白い余白を取り除く関数を書いていました。良い比率が見つからない場合は、この比率を使用することができます。これには、数字をggsaveのpngとして保存し、それをknitr::include_graphics()という次のチャンクで呼び出す必要があります。
機能は、次のいずれかです。

#' A function to crop white margins of a PNG image 
#' 
#' @param x path to the PNG image 
#' @param pixel number of white pixels lines to keep 
#' @export 

Rm_WhiteMargins <- function(x, pixel = 2) 
{ 
    # Cut the output image to remove dirty white margins from corrplot 
    img <- png::readPNG(x) 

    img.test.row <- apply(img, 3, function(layer) { 
    apply(layer, 1, function(i) {(sum(i != 1) > 0)}) 
    }) %>% 
    apply(., 1, function(i) {(sum(i) > 0)}) 
    rowMin <- max(min(which(img.test.row[1:round(length(img.test.row)/2)])) - (1 + pixel), 1) 
    rowMax <- min(max(c(1:length(img.test.row))[ 
    round(length(img.test.row)/2) : length(img.test.row)][ 
     which(img.test.row[(length(img.test.row)/2) : length(img.test.row)])]) + 1 + pixel, 
    length(img.test.row)) 

    img.test.col <- apply(img, 3, function(layer) { 
    apply(layer, 2, function(i) {(sum(i != 1) > 0)}) 
    }) %>% 
    apply(., 1, function(i) {(sum(i) > 0)}) 
    colMin <- max(min(which(img.test.col[1:round(length(img.test.col)/2)])) - (1 + pixel), 1) 
    colMax <- min(max(c(1:length(img.test.col))[ 
    round(length(img.test.col)/2) : length(img.test.col)][ 
     which(img.test.col[(length(img.test.col)/2) : length(img.test.col)])]) + 1 + pixel, 
    length(img.test.col)) 

    # Remove rows and cols with white pixels from the original image 
    img <- img[rowMin:rowMax, colMin:colMax,] 
    png::writePNG(img, target = paste0(gsub(".png", "", x), "_crop.png")) 
    rm(img) 
} 

これは助けることができたら...

+0

助けてくれてありがとうが、私は貯蓄と数字を迂回せずに解決策を好むだろう – Dries

関連する問題