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

May*_*ans 5 r shiny plotly ggplotly

我试图为plotly'ssubplot函数中的每个图表提供单独的标题,我发现了一篇文章,您可以使用它来扩展子图,%>% layout(title = "Main Title)但我想为每个图表提供单独的标题(我正在使用,ggtitle但只绘制最后一个标题提供(图 4)。我发现了类似的帖子为每个子图提供标题 - R Shiny但我不认为我可以facet_wrap在我的场景中。

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

任何帮助表示赞赏!

在此输入图像描述

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)
Run Code Online (Sandbox Code Playgroud)

ism*_*gal 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)
Run Code Online (Sandbox Code Playgroud)

结果

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