0
votes

How would I add the Y axis title "Thousands of People" to cover the entire figure, and not for a single subplot row? The image below shows the y-axis title scrunched at the bottom if the plot. I'm wanting a single y-axis label, oriented in the middle of the overall chart, not the middle of the subplot-row. BONUS: keep the y axis title orientation while allowing the layout(autosize = TRUE).

library(plotly)
  plotList <- function(nplots) {
   lapply(seq_len(nplots), function(x) {
    plot_ly(economics, x = ~date, y = ~unemploy)%>%
      add_annotations(
        text = ~paste("Plot ",x),
        x = 0.5,
        y = 1.1,
        yref = "paper",
        xref = "paper",
        xanchor = "middle",
        yanchor = "top",
        showarrow = FALSE)
  }
    )
}
s1 <- subplot(plotList(4), nrows = 4, shareX = TRUE, shareY = TRUE, titleY = FALSE, margin = .01)
s2 <- subplot(plotList(2), shareY = TRUE)
fig <- subplot(s1, s2, 
               plot_ly(economics, x = ~date, y = ~unemploy)%>%
                 layout(yaxis = list(title = 'Total Unemployed Persons')), 
                        nrows = 3, 
                        margin = 0.04, heights = c(0.6, 0.3, 0.1),
                        titleY = TRUE)

fig

ORIGINAL CHART: enter image description here

DESIRED CHART: enter image description here These links may help anyone trying to answer: r plotly community, and python plotly community

2

2 Answers

0
votes

A similar question has been asked and answered for Python. The solution will be the same for R. Just drop the standard axis labels, and show the desired info as annotations instead. The following figure is produced by the snippet below, and inserts a vertical annotation at y=0.5 and x=-0.2

enter image description here

Complete code:

library(plotly)
fig1 <- plot_ly(economics, x = ~date, y = ~unemploy)
fig1 <- fig1 %>% add_lines(name = ~"unemploy")
fig2 <- plot_ly(economics, x = ~date, y = ~uempmed)
fig2 <- fig2 %>% add_lines(name = ~"uempmed")
fig <- subplot(nrows=2,fig1, fig2)



# make room for both main title and y-axis titles
m <- list(
  l = 100,
  r = 25,
  b = 25,
  t = 125
  #pad = 4
)
fig <- fig %>% layout(title=list(text='Main figure title', font = list(color = "red",size = 24)),
                      autosize = F,
                      #width = 500, height = 500,
                      margin = m)

  
# your axis title
fig <- fig %>% layout(annotations = list(
                list(x = -0.15 , y = 0.5, text = "Thousands of people",
                     font = list(color = "blue",size = 18),
                     textangle = 270,
                     showarrow = F, xref='paper', yref='paper', size=48)
                
                ))

fig
0
votes

I suggest using the 'xshift' parameter to preserve dynamic resizing. Keeping vestland's suggestion the same otherwise, but setting 'x = 0' and 'xshift = -50' in annotations:

fig1 <- plot_ly(economics, x = ~date, y = ~unemploy)
fig1 <- fig1 %>% add_lines(name = ~"unemploy")
fig2 <- plot_ly(economics, x = ~date, y = ~uempmed)
fig2 <- fig2 %>% add_lines(name = ~"uempmed")
fig <- subplot(nrows=2,fig1, fig2)



# make room for both main title and y-axis titles
m <- list(
  l = 100,
  r = 25,
  b = 25,
  t = 125
  #pad = 4
)
fig <- fig %>% layout(title=list(text='Main figure title', font = list(color = "red",size = 24)),
                      autosize = T,
                      #width = 500, height = 500,
                      margin = m)


# your axis title
fig <- fig %>% layout(annotations = list(
  list(x = 0 , y = 0.5, text = "Thousands of people",
       xshift = -50,
       font = list(color = "blue",size = 18),
       textangle = 270,
       showarrow = F, xref='paper', yref='paper', size=48)
  
))

fig

xshift is documented here for reference: https://plotly.com/r/reference/layout/annotations/#layout-annotations-items-annotation-xshift