【问题标题】:On shiny app ggplotly() renders half the size of plot_ly(). How to fix that?在闪亮的应用程序 r plotly() 呈现 plotly() 的一半大小。如何解决?
【发布时间】:2017-07-08 02:49:14
【问题描述】:

当使用 ggplotly 与 plot_ly 在闪亮的应用程序中生成绘图时,绘图的宽度小于一半。这是为什么?有没有办法修复它,以便 ggplotly 生成与 plot_ly 或 ggplot2 宽度相同的图?我尝试过使用 width 参数,但这并不能解决问题。

闪亮应用的输出:

library(shiny)
library(plotly)
library(ggplot2)

ui <- fluidPage(
   titlePanel("plotly with shiny"),
      mainPanel(
        h4("Using ggplotly:"), plotlyOutput("ggplotly", width = "200"),
        h4("Using native plotly:"), plotlyOutput("plotly"),
        h4("Using ggplot:"), plotOutput("ggplot")
      )
)

server <- function(input, output) {
  data <- reactive({
    d = diamonds[sample(nrow(diamonds), 300),]
  }) 
  output$ggplot <- renderPlot({
      ggplot(data(), aes(x=carat, y=price, color=price)) + geom_point()
   })
   output$ggplotly <- renderPlotly({
     v = ggplot(data(), aes(x=carat, y=price, color=price)) + geom_point()
     ggplotly(v)
   })
   output$plotly <- renderPlotly({
     plot_ly(data(), x = ~carat, y = ~price, color = ~price)
   })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    不要使用plotlyOutput的参数width,而使用ggplotly之一:

    ggplotly(v, width=800)
    

    【讨论】:

    • 谢谢斯蒂芬。这对我行得通。只有一个副作用是当浏览器窗口大小改变时,绘图不会自动调整大小。我现在可以忍受。
    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2020-09-30
    • 2021-12-28
    • 1970-01-01
    • 2016-10-06
    • 2021-05-12
    相关资源
    最近更新 更多