12

I am creating a Shiny app with a plotly scatterplot. I am trying to keep the scatterplot square-shaped, but want a much larger size than its default. In the simple MWE below, for instance, I am setting the width and height parameters to 1400px. However, even when I change these values (say to 800px), it does not seem to make any change in the size of the plotly scatterplot.

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

set.seed(1)
dat <- data.frame(ID = paste0("ID", 1:100), x = rnorm(100), y = rnorm(100), stringsAsFactors = FALSE)

ui <- shinyUI(fluidPage(
  titlePanel("title panel"),
  sidebarLayout(position = "left",
  sidebarPanel(width=3,
    actionButton("goButton", "Action")
   ),
  mainPanel(width=9, 
    plotlyOutput("scatMatPlot", width = "1400px", height = "1400px")
  )
  )
))

server <- shinyServer(function(input, output, session) {
  p <- ggplot(data= dat, aes(x=x, y=y)) + geom_point() + coord_cartesian(xlim = c(-5, 5), ylim = c(-5, 5)) + coord_equal(ratio = 1) 
  p2 <- ggplotly(p)
  output$scatMatPlot <- renderPlotly({p2})
})

shinyApp(ui, server)

I tried other size values than "1400px". For instance, I tried "auto" and "100%" - but these did not seem to make a difference either. How can I change the size of the plotly scatterplot in this MWE? Thank you for any input.

  • 1
    Your code works just fine on my side. – Sean Lin Jun 30 '17 at 14:52
  • Thanks for letting me know. So, you can adjust the size of the Plotly scatterplot by changing the values of "1400px" to something else? I tried again with this exact code using FireFox, Safari, and Google Chrome, and changing the values of "1400px" to something else does nothing to the size of the scatterplot Plotly object. Any ideas what could be causing the difference between your use of the code above and my use of the code above? I am stumped! –  Jun 30 '17 at 16:00
  • Have you tried the `height` parameter in `renderPlotly` and `plotlyOutput`? It is `400px` by default and might overwrite the `height` specified in the `ggplot` object. – Gregor de Cillia Jun 30 '17 at 16:46
  • @GreenStone Yes, just by changing values of "1400px". I find that it doesn't work on my Windows RStudio, but does work on my Linux RStudio Server. I guess the reason might be some difference between platform or R version. – Sean Lin Jun 30 '17 at 18:25

2 Answers2

17

When you use ggplotly() you can change the size of plotlyOutput with layout options in the server part:

p2 <- ggplotly(p) %>% layout(height = 800, width = 800)

I found that plotlyOutput will only work with parameters width = "600px", height = "600px" if you provide input directly from plot_ly() instead ggplotly(), e.g.

p2 <- plot_ly(dat, x = ~x, y = ~y)
DianaLog
  • 326
  • 2
  • 9
  • Just for documentation purposes, note that `layout()` is deprecated!: https://stackoverflow.com/questions/56859151/how-to-change-plotly-color-and-get-warning-specifying-width-height-in-layout – latlio Nov 02 '21 at 22:30
4

It seems that the plotlyOutput function does not hand over the height/width parameters. As mentioned before, you can force to plot to be of a certain size:

p <- plot_ly(x = x, y = y, height=800) 

However, if you have following elements on the site (e.g. another plot like in my case) the plot is partially hidden. I found a workaround by manipulating the plotlyOutput object on the server-side. Here is a simple example:

Server:

output$plotly <- renderUI({
  plot_output_list <- lapply(1:3, function(i) {
    plotname <- paste0("plotly", i)
    plot_output_object <- plotlyOutput(plotname)
    plot_output_object <- renderPlotly({
      p <- plot_ly(x = a, y = b)
      return(p) # only necessary when adding other plotly commands like add_trace 
    })
  })
  # for each element set the height (here went something wrong with plotlyOutput)
  for(i in 1:length(plot_output_list)){
    attr(plot_output_list[[i]],'outputArgs') <- list(height="850px")
  }
  # return
  return(plot_output_list)
})

UI:

uiOutput("plotly")

I anyway had to go the way via renderUI since I have a dynamic number of plots. Hope this helps you too

Alexander Leow
  • 476
  • 2
  • 9