有条不紊的plotlyOutput()没有响应高度和宽度大小

时间:2021-01-06 14:26:19

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.

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

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.

我尝试了其他尺寸值而不是“1400px”。例如,我试过“自动”和“100%” - 但这些似乎也没有什么区别。如何更改此MWE中的绘图散点图的大小?感谢您的任何意见。

2 个解决方案

#1


3  

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

当您使用ggplotly()时,您可以使用服务器部分中的布局选项更改plotlyOutput的大小:

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.

我发现如果你直接从plot_ly()而不是ggplotly()提供输入,那么plotlyOutput只能用参数width =“600px”,height =“600px”。

p2 <- plot_ly(dat, x = ~x, y = ~y)

#2


1  

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:

似乎plotlyOutput函数没有移交高度/宽度参数。如前所述,您可以强制绘制一定大小:

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:

但是,如果您在网站上有以下元素(例如我的情况下的另一个图),则该图部分隐藏。我通过在服务器端操作plotlyOutput对象找到了一种解决方法。这是一个简单的例子:

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

无论如何,我必须通过renderUI前进,因为我有一个动态数量的图。希望这对你也有帮助

#1


3  

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

当您使用ggplotly()时,您可以使用服务器部分中的布局选项更改plotlyOutput的大小:

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.

我发现如果你直接从plot_ly()而不是ggplotly()提供输入,那么plotlyOutput只能用参数width =“600px”,height =“600px”。

p2 <- plot_ly(dat, x = ~x, y = ~y)

#2


1  

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:

似乎plotlyOutput函数没有移交高度/宽度参数。如前所述,您可以强制绘制一定大小:

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:

但是,如果您在网站上有以下元素(例如我的情况下的另一个图),则该图部分隐藏。我通过在服务器端操作plotlyOutput对象找到了一种解决方法。这是一个简单的例子:

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

无论如何,我必须通过renderUI前进,因为我有一个动态数量的图。希望这对你也有帮助