使用网格的情节子图中的标题

Titles in plotly subplots using grid

我正在尝试应用 solution for having titles in a plotly subplot to a plotly grid using this Subplots Using Grid example:

library(plotly)
library(dplyr)

f <- list(family="Courier New, monospace",size = 18,color = "black")
fig <- plot_ly()
fig <- fig %>% add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n,
                       name = "Cut", domain = list(row = 0, column = 0)) %>%
  layout(annotations=list(text = "Cut",font = f,xref = "paper",yref = "paper",yanchor = "bottom",xanchor = "center",align = "center",x = 0.5,y = 1,showarrow = FALSE))
fig <- fig %>% add_pie(data = count(diamonds, color), labels = ~color, values = ~n,
                       name = "Color", domain = list(row = 0, column = 1)) %>%
  layout(annotations=list(text = "Color",font = f,xref = "paper",yref = "paper",yanchor = "bottom",xanchor = "center",align = "center",x = 0.5,y = 1,showarrow = FALSE))
fig <- fig %>% add_pie(data = count(diamonds, clarity), labels = ~clarity, values = ~n,
                       name = "Clarity", domain = list(row = 0, column = 2)) %>%
  layout(annotations=list(text = "Clarity",font = f,xref = "paper",yref = "paper",yanchor = "bottom",xanchor = "center",align = "center",x = 0.5,y = 1,showarrow = FALSE))
fig <- fig %>% layout(showlegend = F,grid=list(rows=1, columns=3),
                      xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                      yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

我得到的是:

有什么想法吗?

您可以使用此代码:

f <- list(family="Courier New, monospace",size = 18,color = "black")
  fig <- plot_ly()
  fig1 <- fig %>% add_pie(data = count(diamonds, cut), labels = ~cut, values = ~n,
                         name = "Cut", domain = list(row = 0, column = 0)) %>%
    layout(annotations=list(text = "Cut",font = f,xref = "paper",yref = "paper",yanchor = "bottom",xanchor = "center",align = "center",x = 0.5,y = 1,showarrow = FALSE))
  fig2 <- fig %>% add_pie(data = count(diamonds, color), labels = ~color, values = ~n,
                         name = "Color", domain = list(row = 0, column = 1)) %>%
    layout(annotations=list(text = "Color",font = f,xref = "paper",yref = "paper",yanchor = "bottom",xanchor = "center",align = "center",x = 0.5,y = 1,showarrow = FALSE))
  fig3 <- fig %>% add_pie(data = count(diamonds, clarity), labels = ~clarity, values = ~n,
                         name = "Clarity", domain = list(row = 0, column = 2)) %>%
    layout(annotations=list(text = "Clarity",font = f,xref = "paper",yref = "paper",yanchor = "bottom",xanchor = "center",align = "center",x = 0.5,y = 1,showarrow = FALSE))
  fig <- fig %>% layout(showlegend = F,grid=list(rows=1, columns=3),
                        xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                        yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
  
  p <- subplot(fig1, fig2, fig3, titleX = TRUE, titleY = TRUE) %>%
    layout(showlegend = FALSE, grid=list(rows=1, columns=3),
           xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
           yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

  p 

输出: