Plotly:如何使用 R 和 Shiny 为子图创建单个 y-axis 标签?

Plotly: How to create a single y-axis label for subplots using R and Shiny?

如何添加 Y 轴标题“千人”以覆盖整个图形,而不是单个子图行?下图显示了 y-axis 标题在情节的底部被揉成一团。我想要一个 y-axis 标签,位于整个图表的中间,而不是 subplot-row 的中间。奖励:在允许布局的同时保持 y 轴标题方向(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

原始图表:

所需图表: 这些链接可以帮助任何试图回答的人:r plotly community, and python plotly community

已针对 Python 提出并回答了类似的问题。 R 的解决方案是相同的。只需删除标准轴标签,并将所需信息显示为注释即可。下图由下面的代码片段生成,并在 y=0.5x=-0.2

处插入了一个垂直注释

完整代码:

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

我建议使用 'xshift' 参数来保持动态调整大小。保持 vestland 的建议不变,但在注释中设置 'x = 0' 和 'xshift = -50':

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 在此处记录以供参考:https://plotly.com/r/reference/layout/annotations/#layout-annotations-items-annotation-xshift