r - 格式化 Shiny Plotly 子图 - 单个标题和图形大小

标签 r shiny plotly ggplotly

我试图在 plotlysubplot 函数中为我的每个图表提供单独的标题,我找到了一篇文章,您可以在其中使用 扩展子图%>% layout(title = "Main Title) 但我想要每个图表的单独标题(我正在使用 ggtitle 但它只绘制提供的最后一个标题(图 4) . 我发现了一个类似的帖子 Provide title to each of the subplots - R Shiny 但我不认为我可以在我的场景中 facet_wrap

此外 - 我想知道如何增加子图中图表之间的边距,因为它们似乎真的被压在一起了。

感谢任何帮助!

enter image description here

ui <- fluidPage(
  sidebarPanel("This is a sidebar"),
  mainPanel(plotlyOutput("myplot"))
    )

server <- function(input, output){

  output$myplot <- renderPlotly({

    gg1 <- ggplotly(
      ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
        geom_point() +
        theme_minimal() +
        ggtitle("Plot 1")
    )

    gg2 <- ggplotly(
      ggplot(iris, aes(x=Species, y=Sepal.Length)) +
        geom_boxplot() +
        theme_minimal() +
        ggtitle("Plot 2")
    )

    gg3 <- ggplotly(
      ggplot(iris, aes(x=Petal.Width)) +
        geom_histogram() +
        ggtitle("Plot 3")
    )

    gg4 <- ggplotly(
      ggplot(iris, aes(x=Petal.Length)) +
        geom_histogram() +
        ggtitle("Plot 4")
    )

    subplot(list(gg1,gg2,gg3,gg4), nrows = 2)
  })
}

shinyApp(ui = ui, server = server)

最佳答案

正如@Edo 已经提到的,子图标题是 open issue关于 R 的 plotly api。目前我们需要使用注解。这是一个 official example .

subplot 函数为我们提供了一个参数 margin 来添加一些空间。

library(shiny)
library(plotly)

ui <- fluidPage(
  sidebarPanel("This is a sidebar"),
  mainPanel(plotlyOutput("myplot"))
)

server <- function(input, output, session){

  output$myplot <- renderPlotly({

    gg1 <- ggplotly(
      ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
        geom_point() +
        theme_minimal()
    ) %>% add_annotations(
      text = "Plot 1",
      x = 0,
      y = 1,
      yref = "paper",
      xref = "paper",
      xanchor = "left",
      yanchor = "top",
      yshift = 20,
      showarrow = FALSE,
      font = list(size = 15)
    )

    gg2 <- ggplotly(
      ggplot(iris, aes(x=Species, y=Sepal.Length)) +
        geom_boxplot() +
        theme_minimal()
    ) %>% add_annotations(
      text = "Plot 2",
      x = 0,
      y = 1,
      yref = "paper",
      xref = "paper",
      xanchor = "left",
      yanchor = "top",
      yshift = 20,
      showarrow = FALSE,
      font = list(size = 15)
    )

    gg3 <- ggplotly(
      ggplot(iris, aes(x=Petal.Width)) +
        geom_histogram()
    ) %>% add_annotations(
      text = "Plot 3",
      x = 0,
      y = 1,
      yref = "paper",
      xref = "paper",
      xanchor = "left",
      yanchor = "top",
      yshift = 20,
      showarrow = FALSE,
      font = list(size = 15)
    )

    gg4 <- ggplotly(
      ggplot(iris, aes(x=Petal.Length)) +
        geom_histogram()
    ) %>% add_annotations(
      text = "Plot 4",
      x = 0,
      y = 1,
      yref = "paper",
      xref = "paper",
      xanchor = "left",
      yanchor = "top",
      yshift = 20,
      showarrow = FALSE,
      font = list(size = 15)
    )

    subplot(list(gg1,gg2,gg3,gg4), nrows = 2, margin = 0.06)
  })
}

shinyApp(ui = ui, server = server)

Result

顺便说一句,您可以使用 schema() 浏览 plotly 的属性。

关于r - 格式化 Shiny Plotly 子图 - 单个标题和图形大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59182646/

相关文章:

r - 合并多个data.table

r - 使用百分比时美学中的组论点

r - 将带有实时控件的视频嵌入到交互式 Shiny 环境中

r - ShinyApps.io 与 Github

R react 值 - 添加到列表中的唯一值

javascript - 用 Shiny 的用户输入触发鼠标单击事件

Python 警告 : plotly. graph_objs.Line 已弃用

r - 映射博客之间的链接连接的最佳方式是什么?

python - 如何更改 Dash Graph 的大小?

r - 使用cuDNN内核进行LSTM