4

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: Photo: 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)
Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
smes
  • 41
  • 2

1 Answers1

1

Do not use the argument width of plotlyOutput, and use the one of ggplotly:

ggplotly(v, width=800)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Thanks Stephane. That works for me. Only one side effect is that the plot doesn't auto resize when the browser window size is changed. I can live that for now. – smes Jul 08 '17 at 22:05