首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置闪亮的子图的格式-单个标题和图形大小

设置闪亮的子图的格式-单个标题和图形大小
EN

Stack Overflow用户
提问于 2019-12-05 02:33:12
回答 1查看 1.8K关注 0票数 3

我试着在plotlysubplot函数中为我的每个图形提供单独的标题,我发现了一个帖子,在那里你可以使用%>% layout(title = "Main Title)扩展子图,但我希望每个图形都有单独的标题(我使用ggtitle,但它只绘制提供的最后一个标题(图4)。我找到了一个类似的帖子Provide title to each of the subplots - R Shiny,但我不认为我可以在我的场景中使用facet_wrap

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

感谢任何人的帮助!

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-05 16:38:25

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

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

代码语言:javascript
复制
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)

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59182646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档