Plotly轴标签重叠并且太大

时间:2016-09-07 04:14:41

标签: r plot ggplot2 shiny plotly

我正在使用Rshiny以及ggplot2。当输出刻度标签太大的图(如下所示)时,轴标签与刻度标签重叠,如图所示:

#UI.R
library(shiny)
library(plotly)
library(ggplot2)

shinyUI(fluidPage(
  plotlyOutput("Plot")
 ))

#Server.R

shinyServer(function(input, output) {

  output$Plot <- renderPlotly({

    plot <- ggplot(diamonds, aes(x=cut)) +
               geom_bar()+
               theme(axis.text.x = element_text(angle = 90, size = 25))
    ggplotly(plot)

  })

})

输出:bad plot

您可以看到较大的刻度线与标签重叠(在本例中为“cut”)。虽然我通过使字体更大来制作这个例子,但是长标签会发生同样的事情。此外,有时候整个标签是不可见的(即它会“超出框架”),就像这样(取自不同的项目):overflow plot

我想让这个行为更像ggplot2,输出:good plot

有谁知道这样做的方法?我找不到关于如何改变来自ggplotly的东西的很好的文档。

1 个答案:

答案 0 :(得分:1)

使用plotly_build()调整边距。来自文档:

  

使用此功能可用于覆盖由提供的默认值   ggplotly / plot_ly或用于调试渲染错误。

pb <- plotly_build(plot)
str(pb)
pb$layout$margin$b <- 220
pb

enter image description here