Search code examples
rplotlysubplot

Titles/Labels for Stacked Subplots in R Plotly


I want the legend hidden for this series of plots. Is there a way to add either a x-axis label above the xaxis or y-axis label to the left of the yaxis for each subplot in R Plotly?

I've tried adding them to each plot before they are combined with the subplot function, but only one title/label shows. I've tried the facets in ggplot and then ggplotly but the final plots are not as attractive as the ones made with plotly.

this is sample code:

library(plotly) 

fig1 <- plot_ly(x = c(3, 4, 5), y = c(1000, 1100, 1200), type = 'scatter', mode = 'lines+markers') 

fig2 <- plot_ly(x = c(2, 3, 4), y = c(100, 110, 120), type = 'scatter', mode = 'lines+markers') 

fig3 <- plot_ly(x = c(0, 1, 2), y = c(10, 11, 12), type = 'scatter', mode = 'lines+markers') 

fig <- subplot(fig1, fig2, fig3, nrows = 3, shareX = TRUE) %>% 
  
  hide_legend() %>%
  
  layout(title = list(text = "Stacked Subplots"),
         
         plot_bgcolor='#e5ecf6', 
         
         xaxis = list( 
           
           zerolinecolor = '#ffff', 
           
           zerolinewidth = 2, 
           
           gridcolor = 'ffff'), 
         
         yaxis = list( 
           
           zerolinecolor = '#ffff', 
           
           zerolinewidth = 2, 
           
           gridcolor = 'ffff')) 

fig

Solution

  • figured out how to do it both ways. this adds it to the bottom of the plots:

    fig1 <- plot_ly(x = c(3, 4, 5), y = c(1000, 1100, 1200), type = 'scatter', mode = 'lines+markers') %>% 
      layout(annotations =
               list(x = 1, y = -0.1, text = "Fig1",
                    showarrow = F, xref='paper', yref='paper',
                    xanchor='right', yanchor='bottom', xshift=0, yshift=0,
                    font=list(size=15, color="red")))
    fig2 <- plot_ly(x = c(2, 3, 4), y = c(100, 110, 120), type = 'scatter', mode = 'lines+markers') %>%
      layout(annotations =
               list(x = 1, y = -0.1, text = "Fig2",
                    showarrow = F, xref='paper', yref='paper',
                    xanchor='right', yanchor='bottom', xshift=0, yshift=0,
                    font=list(size=15, color="red")))
    fig3 <- plot_ly(x = c(0, 1, 2), y = c(10, 11, 12), type = 'scatter', mode = 'lines+markers') %>%
      layout(annotations =
               list(x = 1, y = -0.1, text = "Fig3",
                    showarrow = F, xref='paper', yref='paper',
                    xanchor='right', yanchor='bottom', xshift=0, yshift=0,
                    font=list(size=15, color="red")))
    fig <- subplot(fig1, fig2, fig3, nrows = 3, shareX = TRUE) %>% 
      
      hide_legend() %>%
      
      layout(title = list(text = "Stacked Subplots"),
             
             plot_bgcolor='#e5ecf6', 
             
             xaxis = list( 
               
               zerolinecolor = '#ffff', 
               
               zerolinewidth = 2, 
               
               gridcolor = 'ffff'), 
             
             yaxis = list( 
               
               zerolinecolor = '#ffff', 
               
               zerolinewidth = 2, 
               
               gridcolor = 'ffff')) 
    
    fig
    

    this puts the label to the left side of the plots:

    fig1 <- plot_ly(x = c(3, 4, 5), y = c(1000, 1100, 1200), type = 'scatter', mode = 'lines+markers') %>% 
      layout(annotations =
               list(x = -0.1, y = 0.5, text = "Fig1",
                    showarrow = F, xref='paper', yref='paper',
                    xanchor='right', yanchor='bottom', xshift=0, yshift=0,
                    textangle=-90,
                    font=list(size=15, color="red")))
    fig2 <- plot_ly(x = c(2, 3, 4), y = c(100, 110, 120), type = 'scatter', mode = 'lines+markers') %>%
      layout(annotations =
               list(x = -0.1, y = 0.5, text = "Fig2",
                    showarrow = F, xref='paper', yref='paper',
                    xanchor='right', yanchor='bottom', xshift=0, yshift=0,
                    textangle=-90,
                    font=list(size=15, color="red")))
    fig3 <- plot_ly(x = c(0, 1, 2), y = c(10, 11, 12), type = 'scatter', mode = 'lines+markers') %>%
      layout(annotations =
               list(x = -0.1, y = 0.5, text = "Fig3",
                    showarrow = F, xref='paper', yref='paper',
                    xanchor='right', yanchor='bottom', xshift=0, yshift=0,
                    textangle=-90,
                    font=list(size=15, color="red")))
    fig <- subplot(fig1, fig2, fig3, nrows = 3, shareX = TRUE) %>% 
      
      hide_legend() %>%
      
      layout(title = list(text = "Stacked Subplots"),
             
             plot_bgcolor='#e5ecf6', 
             
             xaxis = list( 
               
               zerolinecolor = '#ffff', 
               
               zerolinewidth = 2, 
               
               gridcolor = 'ffff'), 
             
             yaxis = list( 
               
               zerolinecolor = '#ffff', 
               
               zerolinewidth = 2, 
               
               gridcolor = 'ffff')) 
    
    fig