闪亮的plotlyOutput()对高度和宽度的大小不响应 [英] Shiny plotlyOutput() not responding to height and width sizes
问题描述
我正在使用一个散点图创建一个Shiny应用程序.我正在尝试使散点图保持正方形,但要比默认大小大得多.例如,在下面的简单MWE中,我将width和height参数设置为1400px.但是,即使我更改了这些值(例如更改为800px),似乎也不会对散点图的大小进行任何更改.
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"以外的其他尺寸值.例如,我尝试了自动"和"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)
我发现,如果您直接从plot_ly()
而不是ggplotly()
提供输入,例如
,则plotlyOutput
仅适用于参数width = "600px", height = "600px"
.
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)
这篇关于闪亮的plotlyOutput()对高度和宽度的大小不响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!