Search code examples
rshinyflexdashboard

Not able to plot side by side plotly bar plots in R Shiny flexdashboard


I have to design a Shiny flexdashboard and i want to plot two bar plots in plotly a "Single" tab.

What I am trying is the following:

---
title: "My Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: column
    vertical_layout: fill        
---

```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(plotly)
library(plyr)
```


My Page
===================================== 

Column {data-width=260 .tabset}
-----------------------------------------------------------------------

### Tab 1

```{r}

```

### Tab 2

```{r}

```

### Tab 3

```{r}

```

Column {.tabset}
-----------------------------------------------------------------------

### REGION 1

```{r}

# Make some noisily increasing data
set.seed(955)
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))


p1 <- ggplot(dat, aes(x=xvar, y=yvar)) +
            geom_point(shape=1)      # Use hollow circles
ggplotly(p1)


p2 <- ggplot(dat, aes(x=xvar, y=yvar)) +
            geom_point(shape=1) +    # Use hollow circles
            geom_smooth(method=lm)   # Add linear regression line
ggplotly(p2)

```

### REGION 2

```{r}
```

I am not getting plots side by side (1 x 2 i.e. vertically side by side).

Using the above code, I should be able to get two plots in single tab, but actually I am getting one. Also, I would be expecting two plots to be next to each other. But looks like I have some terrible mistake here.

Where am I wrong??


Solution

  • We can use the subplot

    subplot(ggplotly(p1), ggplotly(p2))
    

    -full code


    title: "My Dashboard"
    output: 
      flexdashboard::flex_dashboard:
      orientation: column
    vertical_layout: fill        
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(ggplot2)
    library(plotly)
    library(plyr)   
    ```
    
    
    My Page
    ===================================== 
    
      Column {data-width=260 .tabset}
    -----------------------------------------------------------------------
    
    ### Tab 1
    
    ```{r}
    
    ```
    
    ### Tab 2
    
    ```{r}
    
    ```
    
    ### Tab 3
    
    ```{r}
    
    ```
    
    Column {.tabset}
    -----------------------------------------------------------------------
    
    ### REGION 1
    
    ```{r}
    
    # Make some noisily increasing data
    set.seed(955)
    dat <- data.frame(cond = rep(c("A", "B"), each=10),
                      xvar = 1:20 + rnorm(20,sd=3),
                      yvar = 1:20 + rnorm(20,sd=3))
    
    
    p1 <- ggplot(dat, aes(x=xvar, y=yvar)) +
      geom_point(shape=1)      # Use hollow circles
    
    
    p2 <- ggplot(dat, aes(x=xvar, y=yvar)) +
      geom_point(shape=1) +    # Use hollow circles
      geom_smooth(method=lm)   # Add linear regression line
    
    subplot(ggplotly(p1), ggplotly(p2))
    
    
    ```
    
    ### REGION 2
    
    ```{r}
    ```
    

    -output

    enter image description here