【问题标题】:R Shiny: how to keep the width of the bars the same in ggplot2 even when changing the number of bars?R Shiny:即使更改条数,如何在 ggplot2 中保持条的宽度相同?
【发布时间】:2021-03-12 04:04:18
【问题描述】:

我有一个闪亮的金字塔 ggplot,我可以通过选择输入更改条的数量。但是,当我选择一个较小的数字时,条形的宽度会大量增加以适应绘图的高度。我尝试了几种方法,但都没有成功。

这是我当前用于此特定图表的 UI;

用户界面

mainPanel(fluidRow(column(10,plotOutput("exercise3", height =10000)%>% withSpinner(color="#0dc5c1"))),
                                 box(sliderInput("num", "Number of types of exercises",min = 0, max = 300,step=10,value=c(0,300)), width = 12),
                                 width = 10)

服务器

exercisestat<- reactive({
test2<-dataset[dataset$n%in% seq(from=min(input$num),to=max(input$num)),]

})

output$exercise3<-renderPlot({ggplot(exercisestat(), mapping=aes(x = exercisetype, y = COST, fill = `different costs`)) +
  geom_bar(exercisestat() %>% filter(`Cost Type` == "Fixed Costs"),
           stat = "identity",
           position = "stack",
           width=1,
           mapping=aes(reorder(exercisetype, COST), y=COST)) +
  geom_bar(exercisestat() %>% filter(`Cost Type` == "Variable Costs"),
           stat = "identity",
           position = "stack",
           width=1,
           mapping = aes(y = -COST))+
  coord_flip()+
  geom_hline(yintercept=0) +
  theme_economist(horizontal = TRUE) + scale_y_continuous(position = "right", labels =scales::dollar_format(scale = .001, suffix = "K"))+
  scale_fill_economist() +
  labs(fill = "", x = "", y = "")
},height = function() {200 + (length(exercisestat()))})

我基本上遵循了与Population pyramid w projection in R相同的程序

无论我选择的样本大小(通过改变练习类型的数量),我希望条的宽度保持相同的宽度,我希望根据我的数量增加或减少高度已选中。

如上,我已经尝试过了; height = function() {200 + (length(exercisestat()))}) 但它不起作用,我非常感谢任何帮助。

【问题讨论】:

  • 您能否提供数据和完整的应用程序代码以使此示例可重现?

标签: r ggplot2 shiny height reactive


【解决方案1】:

如果无法重现您的问题,请尝试

geom_bar(position = position_dodge2(width = 0.5, preserve = "single"))

而不是

position = "stack"

您可以使用所需的宽度值。

【讨论】:

  • 谢谢,它可以工作,但它会破坏我的图表。我想把它叠起来,你知道我可以在哪里做这个并把它叠起来吗?谢谢
  • 试试看能不能在这里找到可以用的东西stackoverflow.com/questions/12715635/…
猜你喜欢
  • 2013-03-22
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多