Search code examples
rggplot2r-markdownfacet-wrapflexdashboard

RMarkdown flexdashboard vertical_scroll is not working with facet_wrap


I want the flex dashboard to take up the full screen (full width, full height) and scroll to fit a large number of plots in a facet wrap. Made my issue reproducible using nycflights13, provided below and produces un-readable plots super compressed vertically. how can I achieve this in flex dashboard?

output of knitting below: https://i.sstatic.net/MSecw.jpg

---
title: "facet test"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: scroll
---

```{r setup, include=FALSE}
library(flexdashboard)
library(nycflights13)
library(tidyr)
library(dplyr)
library(ggplot2)
```

Column
-----------------------------------------------------------------------

### Chart A

```{r dfs and plots, include=FALSE, warning=FALSE, cache=FALSE}

df <- flights %>%
  group_by(dest, time_hour) %>%
  summarise(n = n()) %>%
  ungroup()

sp <- ggplot(df, aes(x=time_hour, y=n)) + geom_line()

fr <- sp + facet_wrap(~ dest)
```

```{r  facet, out.width = '100%', warning=FALSE, echo=FALSE, message=FALSE, error=TRUE}
fr
```

Solution

  • You could try experimenting with the fig.height and fig.width options. This gives a fairly reasonable output for me:

    ---
    title: "facet test"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: scroll
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(nycflights13)
    library(tidyr)
    library(dplyr)
    library(ggplot2)
    ```
    
    Column
    -----------------------------------------------------------------------
    
    ### Chart A
    
    ```{r dfs and plots, include=FALSE, warning=FALSE, cache=FALSE}
    
    df <- flights %>%
      group_by(dest, time_hour) %>%
      summarise(n = n()) %>%
      ungroup()
    
    sp <- ggplot(df, aes(x=time_hour, y=n)) + geom_line() 
    
    fr <- 
      sp + 
      facet_wrap(~ dest) +
      theme(strip.text.x = element_text(size = 8))
    ```
    
    ```{r facet, echo=FALSE, error=TRUE, fig.height=20, fig.width=16, message=FALSE, warning=FALSE}
    fr
    ```