【问题标题】:How to change the height of a plot in the shiny context如何在闪亮的上下文中更改绘图的高度
【发布时间】:2020-04-07 13:17:06
【问题描述】:

如下图所示,当在侧边栏中选择多个县时,我的地块高度保持不变。这使得情节过于拥挤,以至于标签变得不可读。我想根据在侧边栏中选择的县的数量来改变我的地块的高度。我该怎么做?

下面,请查看我认为需要修改的部分代码。

界面元素

mainPanel(plotOutput("workforce_plot"), 

服务器元素

workforce_plot <- reactive({
     ggplot(workforce_data(), aes(x =Age_Group, y = Percentage, fill=County.y)) + 
     geom_bar(stat = "identity",position = position_dodge()) +                                                         
     geom_text(
     aes(label = Percentage2),
     vjust = 0.5,
     colour = "black", 
     position = position_dodge(width=0.9),
     fontface = "bold",
     size=3.2,
     hjust = 0)+
     ylim(0,110)+        
     coord_flip()+

     labs(
          x = "Age Groups",
          y = "Percentage",
          face = "bold")
          })

   output$workforce_plot <- renderPlot ({ 
            workforce_plot()
          })

【问题讨论】:

标签: r shiny


【解决方案1】:

您可以将height 参数添加到您的renderPlot 函数中,并将一个函数传递给它,该函数根据一些相关的反应调整高度。

一个例子:

output$workforce_plot <- renderPlot ({ 
            workforce_plot()
          }, height = function() {200 + (length(workforce_data()[["County.y]]) *.2)})

(也可以是 length(input$country) 之类的东西,而不是反应式表达式。)

然后在ui.R 中为该面板定义height = 'auto'

【讨论】:

  • 谢谢!这对我来说非常有用!我可以对标签做同样的事情吗?另外,我想知道是否有任何方法可以在每个年龄段的条形之间添加一个微小的间隙,以便在选择多个县时标签变得足够可读?请参阅上面更新的情节。
  • @Nader - 要获得闪避条之间的间隙,您可以更改闪避宽度,例如:geom_bar(stat = "identity", position = position_dodge2(width = .9, preserve = "single"))
  • 谢谢!我添加了建议的代码,但没有做任何更改。
猜你喜欢
  • 2015-02-09
  • 2020-02-16
  • 1970-01-01
  • 2016-10-22
  • 2021-02-13
  • 1970-01-01
  • 2021-05-01
  • 2012-10-10
  • 2020-01-17
相关资源
最近更新 更多