【问题标题】:ggplotly-size fixed in shiny? Resizing doesnt workggplotly-size 固定在闪亮?调整大小不起作用
【发布时间】:2017-12-27 05:18:53
【问题描述】:

我尝试为我的目的改装this闪亮的应用程序。因为我想在上面添加一些文本,所以我使用 Rmd 格式。以下代码与链接中的代码完全相同,只是我删除了 server- 和 ui-functions,否则您将看不到所有输出。 (更新:我还去掉了点击、缩放和画笔事件,以使代码更具可读性)

```{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
  })

```

我将height=700px 添加到plotlyOutput,因为我的ggplot-graph 中有一个更大的数据集和一个很长的图例。然而,虽然 plotly 图调整 - ggplot-graph 不会大于〜500px。

根据this thread,这是ggplotly 的问题。问题是我需要在plotlyOutput 中设置height=700px 以阻止ggplotly-graph 过度绘制我的文本输出。 我正在搜索的是:

  1. 除了在plotlyOutput 中设置height=700px 之外,另一种停止过度绘图的方法,或者,
  2. 另一种更改ggplotly-graph 大小的方法

为了实现后者,我尝试在之后将ggplotly 的高度添加到布局函数中。我什至在对应的layoutset autosize=FALSE。我也试过specify the height in an extra renderUI-function

【问题讨论】:

    标签: r shiny r-markdown plotly


    【解决方案1】:

    您可以在运行 ggplotly 后更改 plotly 属性。不知道你的意思是不是调整布局,但如果是这样,那么我无法复制你的问题,因为它绝对可以超过 500。

    例如,在这种情况下:

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

    您可以测试差异:plotly 为 500,ggplotly 为 700(不要在 plotlyOutput 本身中设置高度)

      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)
        }
      })
    

    编辑:ggplotly 和 plotly 具有相同的大高度并且不重叠文本输出,前提是 ui 中的高度大于 ggplotly 布局参数中的高度:

      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)
    

    【讨论】:

    • 但是,我不明白为什么 ggplotly(p) %>% layout(dragmode = "select", height = 700) 对你不起作用
    • 它确实有效。但它没有回答这个问题。这个解决方案的问题是,ggplotly 过度绘制了output$hover-object。
    • layout(dragmode = "select", height = 700) 和 plotlyOutput("plot", height = 700) 都试是一样的吗?我尝试了更大的地块,它们调整得很好。
    • 谢谢!你的更新成功了。我将稍微编辑您的代码以澄清。
    • 奇怪的是我以前尝试过这样的事情。但我认为layoutplotOutput 中的heightwidth 值需要相同以防止过度绘制。此外,我还尝试将height- 和width-statements 放入ggplotly 而不是layout。但我想这会被覆盖。
    猜你喜欢
    • 2020-06-04
    • 2016-09-11
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 2015-11-16
    • 2021-05-31
    相关资源
    最近更新 更多