[英]Dynamic height of shiny ggplotly plot
I have a ggplotly
output based on geom_col()
for which the number of bars varies significantly.我有一个基于
geom_col()
的ggplotly
输出,其中条的数量变化很大。 I'd like to configure it so the spacing between bars (and therefore also the bar width) remains constant no matter how many bars there are, which means changing the plot height.我想对其进行配置,以便无论有多少条,条之间的间距(因此也是条宽度)保持不变,这意味着改变绘图高度。 Below is based on this solution but has no effect for me.
以下基于此解决方案,但对我没有影响。 Any suggestions appreciated.
任何建议表示赞赏。
require(tidyverse)
require(shiny)
require(plotly)
ui = fluidPage(
sidebarPanel(width = 3,
sliderInput('count', 'count', min = 3, max = 100, value = 100, step = 25)
),
mainPanel(width = 9,
div(plotlyOutput("plot", height = '200vh'),
style='height:90vh !important; overflow:auto !important; background-color:yellow;')
)
)
server <- function(input, output, session) {
output$plot = renderPlotly({
d = data.frame(x = head(sentences, input$count), y = rlnorm(input$count, meanlog = 5))
p = d %>% ggplot(aes(fct_reorder(x, y), y)) +
geom_col(width = 0.1, col='grey90') + geom_point(size = 2) +
coord_flip() +
theme_minimal(base_size = 12) + theme(panel.grid.major.y = element_blank())
pltly = ggplotly(p) %>% layout(xaxis = list(side ="top" ))
pltly$height = nrow(d) * 15
pltly
})
}
shinyApp(ui = ui, server = server,
options = list(launch.browser = FALSE))
You can specify width/height in ggplotly()
or plot_ly()
:您可以在
ggplotly()
或plot_ly()
指定宽度/高度:
library(tidyverse)
library(shiny)
library(plotly)
ui = fluidPage(
sidebarPanel(width = 3,
sliderInput('count', 'count', min = 3, max = 100, value = 100, step = 25)
),
mainPanel(width = 9,
plotlyOutput("plot"),
)
)
server <- function(input, output, session) {
output$plot = renderPlotly({
d = data.frame(x = head(sentences, input$count), y = rlnorm(input$count, meanlog = 5))
p = d %>% ggplot(aes(fct_reorder(x, y), y)) +
geom_col(width = 0.1, col='grey90') + geom_point(size = 2) +
coord_flip() +
theme_minimal(base_size = 12) + theme(panel.grid.major.y = element_blank())
pltly = ggplotly(p, height = nrow(d) * 15) %>% layout(xaxis = list(side ="top" ))
pltly
})
}
shinyApp(ui = ui, server = server, options = list(launch.browser = TRUE))
However, you might want to specify a bigger minimum height, using the first option, the plot becomes quite narrow.但是,您可能想要指定更大的最小高度,使用第一个选项,绘图变得非常狭窄。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.