Search code examples
rshinyflexdashboardfacet-wrap

How to increase height of Plot to occupy full height space in flexdashboard shiny in r?


I am using tidytuesday UN votes dataset and trying to adjust a facet plot by nrows to occupy the full height in flexdashboard of the plot but it's hardly utilizing half of the space and making the plot less visible.

enter image description here

Alternative is I can make 5 different plots but this will run code for 5 times when it can be done in once with facet.

I have also tried facet_grid, par(mfrow = c(1,1)) but both didn't help.

Code:

library(flexdashboard)
library(shiny)
library(tidyverse)
library(scales)
library(glue)
library(countrycode)
library(tidytuesdayR)

remotes::install_github("davidsjoberg/ggstream") # for creating streamgraph

Data:

tt <- tt_load("2021-03-23")
unvotes <- tt$unvotes

head(unvotes)
   rcid country       country_code vote  vote_number date       amend
  <dbl> <chr>         <chr>        <chr>       <dbl> <date>     <dbl>
1     3 United States US           yes             1 1946-01-01     1
2     3 Canada        CA           no             -1 1946-01-01     1
3     3 Cuba          CU           yes             1 1946-01-01     1
4     3 Haiti         HT           yes             1 1946-01-01     1
5     3 Dominican Re~ DO           yes             1 1946-01-01     1
6     3 Mexico        MX           yes             1 1946-01-01     1

flexdashboard

    ---
title: "UN Country Votes"
output:
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    theme: space
runtime: shiny
resource_files:
- .RData
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
library(scales)
library(glue)
library(countrycode)
library(ggstream)    
library(wesanderson)
```   

Trend {data-icon="fa-bar-chart"}
=====================================

Inputs {.sidebar}
-----------------------------------------------------------------------



Column {data-width=550}
-----------------------------------------------------------------------

### UN Vote Trend over the years

```{r}
# Space for other plot
```


Column {data-width=450}
-----------------------------------------------------------------------

### UN Vote Trend by Continents

```{r}

# par(mfrow = c(1,1))
 unvotes %>%
  mutate(continent = countrycode(country_code, "iso2c", "continent")) %>%
  # mutate_all(as.factor) %>%
  group_by(continent, years = year(date) , vote) %>%
  summarise(count = n(), .groups = "drop_last") %>%
  na.omit() %>%
  # mutate(pct = count/sum(count)) %>%


  ggplot(aes(x = years, y = count, fill = vote)) +
  ggstream::geom_stream(show.legend = FALSE) +
  geom_stream_label(aes(label = vote)) +
  scale_y_continuous(labels = comma, breaks = seq(-2000,2000, 500)) +
  scale_fill_manual(values = wes_palette("Darjeeling2")) +
  theme(panel.grid.major = element_blank()) +
  facet_wrap(~continent, nrow = 5) +

  labs(title = "World UN Voting trend over the years by continent",
       y = "", x = "",
       caption = "created by ViSa")
```

Solution

  • Use renderPlot to wrap your plot so it will be responsive:

    ---
    title: "UN Country Votes"
    output:
        flexdashboard::flex_dashboard:
        orientation: columns
    vertical_layout: fill
    theme: space
    runtime: shiny
    resource_files:
        - .RData
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(shiny)
    library(tidyverse)
    ```
    
    # Trend {data-icon="fa-bar-chart"}
    
    ## Inputs {.sidebar}
    
    ## Column {data-width="550"}
    
    ### UN Vote Trend over the years
    
    ```{r}
    # Space for other plot
    ```
    
    ## Column {data-width="450"}
    
    ### UN Vote Trend by Continents
    
    ```{r}
    
    # par(mfrow = c(1,1))
    renderPlot ({
    ggplot(iris) +
        geom_point(aes(Sepal.Length, Sepal.Width))
    })
    ```
    
    
    

    I simplified your code with a minimum example.