0

I'm using ggplot within my Rmarkdown to plot a number of graphs. I'm facing two issues when I knit the markdown to HTML

  1. I have placed the legend at the top - some of the legends at the left and right most sides are appearing cropped

  2. The width of the plot looks too small for my screen - how do I tell Rmarkdown to expand to a larger width ?

Update: Adding the Rmd file contents.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE)
library('knitr')
options(scipen=999)
library(rlang)
library(reshape2)
library(tidyr)
library(plyr)
library(dplyr)
library(ggplot2)
library(plotly)
library(ggthemes)
library(scales)
library(stringr)
library(RColorBrewer)
library(data.table)
library(forcats)
```


```{r}
text <- "
model,var,value,var2
M1,a,12211,a
M1,b1,10.21,B
M1,b2,5.07,B
M1,c1,41.8,C
M1,c2,58.2,C
M1,d,1.6,d
M2,a,11922,a
M2,b1,15.6,B
M2,b2,8.9,B
M2,c1,38.1,C
M2,c2,61.9,C
M2,d,1.8,d
M2,a,13101,a
M2,b1,9.21,B
M2,b2,6.56,B
M2,c1,36.07,C
M2,c2,63.93,C
M2,d,1.75,d
"
dft <- read.table(textConnection(text), sep=",", header = T)
```

```{r}
text <- "
MODEL,ENGINE,var,value,label,var2
ABCD A1601 F1S - QU1234,QUOINOK,varA_XX_Xxxx,11989,11989,varA_XX_Xxxx
ABCD A1601 F1S - QU1234,QUOINOK,varB_XX_xxxXx,1.87,1.87,varB_XX_xxxXx
ABCD A1601 F1S - QU1234,QUOINOK,varC,1.61,1.61,varC
ABCD A1601 F1S - QU1234,QUOINOK,varD_XXX_YYYYYYY_Zzz,0,0,VAR DEFH Xxxxxxx (%)
ABCD A1601 F1S - QU1234,QUOINOK,varE_XXX_YYYYYYY_Zzz,42.4,42.4,VAR DEFH Xxxxxxx (%)
ABCD A1601 F1S - QU1234,QUOINOK,varF_XXX_YYYYYYY_Zzz,26.6,26.6,VAR DEFH Xxxxxxx (%)
ABCD A1601 F1S - QU1234,QUOINOK,varH_XXX_YYYY_Zzz,31,31,VAR DEFH Xxxxxxx (%)
ABCD A1601 F1S - QU1234,QUOINOK,varG_XXX_YY_ZZZZ,3.4,3.4,VAR GIJK Xxxx (%)
ABCD A1601 F1S - QU1234,QUOINOK,varI_XXXX_YY_ZZZZZ,9.3,9.3,VAR GIJK Xxxx (%)
ABCD A1601 F1S - QU1234,QUOINOK,varJ_XXXX_Yyyy_ZZ_ZZZZZ,12.5,12.5,VAR GIJK Xxxx (%)
ABCD A1601 F1S - QU1234,QUOINOK,varK_Xxxx_YY_ZZZZZ,0,0,VAR GIJK Xxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varA_XX_Xxxx,10357,10357,varA_XX_Xxxx
ABCD CPH1609 F3 - QU1234T,QUOINOK,varB_XX_xxxXx,1.71,1.71,varB_XX_xxxXx
ABCD CPH1609 F3 - QU1234T,QUOINOK,varC,1.62,1.62,varC
ABCD CPH1609 F3 - QU1234T,QUOINOK,varD_XXX_YYYYYYY_Zzz,0,0,VAR DEFH Xxxxxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varE_XXX_YYYYYYY_Zzz,36.3,36.3,VAR DEFH Xxxxxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varF_XXX_YYYYYYY_Zzz,34,34,VAR DEFH Xxxxxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varH_XXX_YYYY_Zzz,29.7,29.7,VAR DEFH Xxxxxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varG_XXX_YY_ZZZZ,3.4,3.4,VAR GIJK Xxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varI_XXXX_YY_ZZZZZ,9.3,9.3,VAR GIJK Xxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varJ_XXXX_Yyyy_ZZ_ZZZZZ,13.6,13.6,VAR GIJK Xxxx (%)
ABCD CPH1609 F3 - QU1234T,QUOINOK,varK_Xxxx_YY_ZZZZZ,0,0,VAR GIJK Xxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varA_XX_Xxxx,12688.5,12688,varA_XX_Xxxx
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varB_XX_xxxXx,1.87,1.87,varB_XX_xxxXx
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varC,1.7,1.7,varC
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varD_XXX_YYYYYYY_Zzz,0,0,VAR DEFH Xxxxxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varE_XXX_YYYYYYY_Zzz,32.3,32.3,VAR DEFH Xxxxxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varF_XXX_YYYYYYY_Zzz,29.8,29.8,VAR DEFH Xxxxxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varH_XXX_YYYY_Zzz,37.9,37.9,VAR DEFH Xxxxxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varG_XXX_YY_ZZZZ,3.4,3.4,VAR GIJK Xxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varI_XXXX_YY_ZZZZZ,9.7,9.7,VAR GIJK Xxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varJ_XXXX_Yyyy_ZZ_ZZZZZ,11,11,VAR GIJK Xxxx (%)
ABCD CPH1613 F3 - MOL2345,MOLTOVAST,varK_Xxxx_YY_ZZZZZ,0,0,VAR GIJK Xxxx (%)
"
dft <- read.table(textConnection(text), sep=",", header = T)

p <-  ggplot(dft, aes(x=MODEL, y=value, fill=var, label=label)) +
      geom_col(aes(col = ENGINE), position=position_dodge(width = 0.9),
                   size=1.2) +
    geom_text(position = position_dodge(width = 1),
               show.legend = FALSE,
              size = 3.5,
              vjust=1
              ) +
    facet_wrap( ~ var2, scales = "free_y", ncol = 1, drop = T) +
    theme_custom_col +
    scale_fill_brewer(palette = "Set3") + 
    scale_color_brewer(palette = "Paired") +
    theme(
      text = element_text(size=ggplotAxesLabelSize),
      legend.position="top",
      axis.text.x=element_text(angle = 20),
      axis.text.y=element_blank()
      ) +
    labs(y = "")
p
```

This gives the following plot in the HTML ( copied image from HTML). As it can be seen the legend portion is appearing cropped.

enter image description here

user3206440
  • 4,749
  • 15
  • 75
  • 132
  • You may solve this by setting chunk options like `fig.width` and/or `out.width` appropriately. Have a look at the Rmarkdown reference guide: https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf – Martin C. Arnold Apr 16 '18 at 07:32
  • Can't help with 1. without a reproducible example. 2. is a [duplicate](https://stackoverflow.com/questions/39634520/specify-height-and-width-of-ggplot-graph-in-rmarkdown-knitr-output/39634521#39634521). – Axeman Apr 16 '18 at 07:34
  • @Axeman what determines the width of the plots within the html - is there a way I can tell Rmd to scale it to the full width of the html ? – user3206440 Apr 16 '18 at 09:42
  • Like M.A. said, play around with the `fig.width` and `out.width` chunk options. – Axeman Apr 16 '18 at 09:44
  • @Axeman - I added a reproducible example to illustrate the issue. – user3206440 Apr 16 '18 at 10:53
  • Possible duplicate of [Plot size and resolution with R markdown, knitr, pandoc, beamer](https://stackoverflow.com/questions/17963962/plot-size-and-resolution-with-r-markdown-knitr-pandoc-beamer) – Michael Harper Apr 16 '18 at 11:09

0 Answers0