2

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

However, I didn't understand why ggplotly(p) %>% layout(dragmode = "select", height = 700) didn't work for you
It does work. But it doesnt answer the question. The issue with this solution is, that the ggplotly is overplotting the output$hover-object.
Is it the same if you try both layout(dragmode = "select", height = 700) and plotlyOutput("plot", height = 700)? I tried bigger plots, they adjusted fine.
Thanks! Your update did the trick. I will just edit your code a bit to clarify.
It is weird I tried something like this before. But I suppose the height and width values in layout and plotOutput need to be the same to keep it from overplotting. In addition I also tried putting height- and width-statements into ggplotly instead of layout. But I guess that gets overwritten.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.