简体   繁体   中英

On shiny app ggplotly() renders half the size of plot_ly(). How to fix that?

A plot's width is less than half when using ggplotly vs plot_ly to generate a plotly in a shiny app. Why is that? Is there a way to fix it so that ggplotly produces the plot with the same width as plot_ly or ggplot2? I have tried using the width parameter but that doesn't fix it.

Output from the shiny app: 照片:闪亮的应用程序输出

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)

不要使用plotlyOutput的参数width ,并使用ggplotly的一个:

ggplotly(v, width=800)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM