Search code examples
rggplot2flexdashboard

FlexDashboard - ggplot width to fill cell width


Is there a way to get ggplot to fill the width within a flexdashboard cell, without specifying the fig.width, so that it is responsive?

In the example below, I'd like the single chart in each cell to fill from left to right, so the one in the top row would be much wider and the two below a little wider.

enter image description here

---
title: "Test Flex Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
---
  
```{r include=FALSE}
pacman::p_load(tidyverse)

```

Raw Data
===================================== 
  
Row
-------------------------------------
  
### **Upper Row** 
  
```{r}

ggplot(mtcars, aes(x = row.names(mtcars), y = mpg)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) +
  ggtitle("MT Cars")

```

Row
---------------------------
  
### **Lower Row, Col 1** 
  
  
```{r}

ggplot(mtcars, aes(x = row.names(mtcars), y = mpg)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) +
  ggtitle("MT Cars")

```

### **Lower Row, Col 2** 

```{r}

ggplot(mtcars, aes(x = row.names(mtcars), y = mpg)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) +
  ggtitle("MT Cars")

```

Solution

  • This can be done with inline CSS! I know you said without fig.width settings, but I gave fig.width a default of 20(for my screen resolution), to let it fill the top column, but with the below inline CSS code...

    ```{css, echo=FALSE}
    .fluid-row {
      font-size: 5.9vw;
    }
    ```
    

    This gives the fluid row a responsive effect. so here is my full screen of the rendered flexdashboard.

    enter image description here

    and then here is when I drag the window on my desktop to a smaller window

    enter image description here

    below is your code with my solution, please let me know if you have issues. Thank you

    ---
    title: "Test Flex Dashboard"
    output: 
      flexdashboard::flex_dashboard:
        orientation: rows
    ---
    
    ```{css, echo=FALSE}
    .fluid-row {
      font-size: 5.9vw;
    }
    ```
      
    ```{r include=FALSE}
    pacman::p_load(tidyverse)
    
    ```
    
    Raw Data
    ===================================== 
      
    Row
    -------------------------------------
      
    ### **Upper Row** 
      
    ```{r, fig.width=20}
    
    ggplot(mtcars, aes(x = row.names(mtcars), y = mpg)) +
      geom_bar(stat = "identity") +
      theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) +
      ggtitle("MT Cars")
    
    ```
    
    Row
    ---------------------------
      
    ### **Lower Row, Col 1** 
      
      
    ```{r}
    
    ggplot(mtcars, aes(x = row.names(mtcars), y = mpg)) +
      geom_bar(stat = "identity") +
      theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) +
      ggtitle("MT Cars")
    
    ```
    
    ### **Lower Row, Col 2** 
    
    ```{r}
    
    ggplot(mtcars, aes(x = row.names(mtcars), y = mpg)) +
      geom_bar(stat = "identity") +
      theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5)) +
      ggtitle("MT Cars")
    
    ```