闪亮的 plotlyOutput() 不响应高度和宽度大小宽度、高度、大小、plotlyOutput

2023-09-06 08:04:37 作者:听 ╮海哭的声音≡゜

我正在创建一个带有散点图的闪亮应用程序.我试图保持散点图为方形,但想要比默认尺寸大得多.例如,在下面的简单 MWE 中,我将宽度和高度参数设置为 1400 像素.但是,即使我更改了这些值(比如 800px),它似乎也没有对 plotly 散点图的大小做出任何改变.

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)

我尝试了除1400px"之外的其他尺寸值.例如,我尝试了auto"和100%"——但这些似乎也没有什么不同.如何更改此 MWE 中散点图的大小?感谢您的任何意见.

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.

推荐答案

当你使用 ggplotly() 时,你可以在服务器中通过布局选项改变 plotlyOutput 的大小部分:

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)

我发现 plotlyOutput 仅适用于参数 width = "600px", height = "600px" 如果您直接从 plot_ly() 改为 ggplotly(),例如

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)