5th
5th

Reputation: 2375

ggplotly-size fixed in shiny? Resizing doesnt work

I try to refit this shiny app for my purposes. Since I want to add some text above it, I use the Rmd-Format. The following code is completely the same as in the link, except that I deleted the server- and ui-functions, because otherwise you dont see all the output. (Update: I also threw out the click,zoom and brush-events, to make the code more readable)

```{r cars, echo=FALSE}
library(plotly)
library(shiny)

fluidPage(
  radioButtons("plotType", "Plot Type:", choices = c("ggplotly", "plotly")),
  plotlyOutput("plot", height = "700px"),
  verbatimTextOutput("hover")
)

  output$plot <- renderPlotly({
    # use the key aesthetic/argument to help uniquely identify selected observations
    key <- row.names(mtcars)
    if (identical(input$plotType, "ggplotly")) {
      p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) + 
        geom_point()
      ggplotly(p) %>% layout(dragmode = "select")
    } else {
      plot_ly(mtcars, x = ~mpg, y = ~wt, key = ~key) %>%
        layout(dragmode = "select")
    }
  })

  output$hover <- renderPrint({
    d <- event_data("plotly_hover")
    if (is.null(d)) "Hover events appear here (unhover to clear)" else d
  })

```

I added height=700px to plotlyOutput, because I have a bigger dataset and a long legend in my ggplot-graph. However while the plotly graph adjusts - the ggplot-graph does not get bigger than ~500px.

According to this thread, this is an issue of ggplotly. The problem is that I need to set height=700px in plotlyOutput to stop the ggplotly-graph from overplotting my Text-output. What I am searching is either:

  1. Another way to stop the overplotting than setting height=700px in plotlyOutput, or alternatively,
  2. Another way to change the size of the ggplotly-graph

To achieve the latter I tried adding the height to ggplotly to the layout function afterwards. I even set autosize=FALSE in the corresponding layout. I also tried to specify the height in an extra renderUI-function.

Upvotes: 2

Views: 1910

Answers (1)

Asayat
Asayat

Reputation: 633

You can change plotly attributes after running ggplotly. Not sure if you meant that as adjusting layouts though, but if so, then I couldn't replicate your problem as it gets more than 500 absolutely fine.

For instance, in this case:

pt<-ggplotly(p) %>% layout(dragmode = "select")
# have a look to all attributes
str(pt)

#Specifically height is
pt$height<-700

You can test the difference: 500 for plotly and 700 for ggplotly (don't set up height in plotlyOutput itself)

  output$plot <- renderPlotly({
    # use the key aesthetic/argument to help uniquely identify selected observations
    key <- row.names(mtcars)
    if (identical(input$plotType, "ggplotly")) {
      p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) + 
        geom_point()
      pt<- ggplotly(p) %>% layout(dragmode = "select")
      pt$height<-700
      pt
      # ggplotly(p) %>% layout(dragmode = "select")
    } else {
      plot_ly(mtcars, x = ~mpg, y = ~wt, key = ~key) %>%
        layout(dragmode = "select",height = 500)
    }
  })

EDIT: ggplotly and plotly have the same big height and don't overlap the text output provided that the height in ui is bigger than height in ggplotly layout argument:

  output$plot <- renderPlotly({
    key <- row.names(mtcars)
    if (identical(input$plotType, "ggplotly")) {
      p <- ggplot(mtcars, aes(x = mpg, y = wt, colour = factor(vs), key = key)) + 
        geom_point()
      ggplotly(p) %>% layout(dragmode = "select", height=800)
    } else {
      plot_ly(mtcars, x = ~mpg, y = ~wt, key = ~key) %>%
        layout(dragmode = "select")
    }
  })

plotlyOutput("plot", height = 800)

enter image description here

Upvotes: 1

Related Questions