格式化Shiny Plotly子图 - 单独的标题和图形大小

5
我想为plotlysubplot函数中的每个图表提供单独的标题。我找到了一篇文章,其中介绍了如何使用%>% layout(title = "Main Title)扩展subplot,但我想为每个图表提供单独的标题(我正在使用ggtitle,但它只会绘制最后一个提供的标题(Plot 4)。我发现了一篇类似的文章Provide title to each of the subplots - R Shiny,但我不认为在我的情况下可以使用facet_wrap
此外,我想知道如何增加subplot中图表之间的边距,因为它们看起来非常挤在一起。
任何帮助都将不胜感激!

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)

根据这个答案:https://dev59.com/sloU5IYBdhLWcg3wnX1O你不能为每个图设置标题.. 你可以使用annotation来完成一些工作..你需要将所有4个图保持在一起吗? - Edo
1个回答

4
如@Edo所提到的,子图标题是R的plotly api中的一个未解决问题。目前我们需要使用注释。这里有一个官方示例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的属性。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接