【发布时间】: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