为什么填充不能在我闪亮的应用程序中的ggplot中起作用?

问题描述 投票:0回答:1

我正在尝试将以下代码复制到我的闪亮应用程序中:

df1 <- df %>% 
  group_by(xyz, inception_month) %>% 
  summarise(policies = sum(policies))

a <- ggplot(df1, aes(x=as.factor(inception_month), y = policies,  fill = xyz)) +
  geom_bar(position = "fill", stat = "identity") + 
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  xlab("Inception Month") +
  ylab("Policy Count")
print(a)

这将为我提供以下期望的输出:enter image description here

但是我无法使用以下动态代码显示填充:

server <- function(input, output) {

df1 <- reactive({df %>% group_by(input$rating_factor, inception_month) %>% summarise(policies = sum(policies))})

  output$plot <- renderPlot({
    p <- ggplot(data = df1(), aes(x=as.factor(inception_month), y = policies, fill = input$rating_factor)) +
      geom_bar(position = "fill", stat = "identity") +
    theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
      xlab("Inception Month") +
      ylab("Policy Count")

    print(p)
  })
ggplot2 shiny shinydashboard
1个回答
0
投票
input$rating_factor创建一个字符值,这不是ggplot期望的值。您需要改用!!sym(input$rating_factor)
© www.soinside.com 2019 - 2024. All rights reserved.