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

时间:2019-12-04 18:33:11

标签: r shiny plotly ggplotly

我试图在plotly的{​​{1}}函数中为每个图形提供单独的标题,但我发现了一个帖子,您可以在其中使用subplot扩展子图,但是我想就像每个图的标题一样(我使用的是%>% layout(title = "Main Title),但仅绘制了提供的最后一个标题(图4)。我发现了类似的帖子Provide title to each of the subplots - R Shiny,但我不认为我可以{{ 1}}。

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

任何帮助表示赞赏!

enter image description here

ggtitle

1 个答案:

答案 0 :(得分:1)

如@Edo子图标题所提到的,是关于R的api的open issue。当前,我们需要使用注释。这是official example

user@libserver.libsoft.in /$ python3.8 -V Python 3.8.0 user@libserver.libsoft.in /$ which python3.8 /usr/local/bin/python3.8 user@libserver.libsoft.in /$ python3.8 -m venv /home/LPython/my_env user@libserver.libsoft.in /$ cd /home/LPython/my_env user@libserver.libsoft.in /home/LPython/my_env$ source bin/activate 函数为我们提供了一个参数subplot,以增加一些空间。

margin

Result

通过这种方式,您可以使用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) 浏览plotly的属性。