【问题标题】:ggplot bar chart does not show correctly on Shinyggplot 条形图在 Shiny 上无法正确显示
【发布时间】:2021-05-09 12:37:34
【问题描述】:

当我在 R 中创建下面的条形图时,没有问题,但是当我将这些代码转换为闪亮的函数时,出现了问题,你能帮忙吗?

普通 R 代码中的条形图:

path <- "WA_Fn-UseC_-HR-Employee-Attrition.csv (job attrition)"
data <-fread(path)
# bar chart function function
Categorical_vs_categorical_plot_2 <- function(data,group_col,fill_col){
  data %>%
    ggplot(aes_(x = fill_col, group = group_col)) + 
    geom_bar(aes(y = ..prop.., fill = factor(..x..)), 
             stat="count", 
             alpha = 0.7) +
    geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), 
              stat= "count", 
              vjust = 2) +
    labs(y = "Percentage", fill= "Education") +
    facet_grid(~Attrition) +
    theme_minimal()+
    theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
    ggtitle("Attrition") 
  
}

Categorical_vs_categorical_plot_2(data,~Attrition,~BusinessTravel)

Normal bar chart

条形图转换为闪亮代码[发生错误,不像上面的普通条形图那样显示 facet_grid 和文本值]:

      output$cat_vs_cat_chart2 <- renderPlot({
    data() %>%
      #ggplot(aes_(x = input$cat_compare, group = ~Attrition)) + 
      ggplot(aes_(x = 'BusinessTravel', group = ~Attrition)) +
      geom_bar(aes(y = ..prop.., fill = factor(..x..)), 
               stat="count", 
               alpha = 0.7) +
      geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), 
                stat= "count", 
                vjust = 2) +
      #labs(y = "Percentage", fill= "Education") +
      facet_grid(~Attrition) +
      theme_minimal()+
      theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
      ggtitle("Attrition") 
    
  })

Error bar chart

【问题讨论】:

  • 请将您自己的解决方案作为答案发布(并标记为已接受),不要将其添加到问题中。
  • 谢谢,我的答案如下!

标签: r ggplot2 shiny


【解决方案1】:

[更新]我自己得到了答案:

[版本1]如果直接输入柱状图的特征:需要在每列特征中添加~:

      output$cat_vs_cat_chart2 <- renderPlot({
    data() %>%
      ggplot(aes_(x = ~BusinessTravel, group = ~Attrition)) +
      geom_bar(aes(y = ..prop.., fill = factor(..x..)), 
               stat="count", 
               alpha = 0.7) +
      geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), 
                stat= "count", 
                vjust = 2) +
      #labs(y = "Percentage", fill= "Education") +
      facet_grid(~Attrition) +
      theme_minimal()+
      theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
      ggtitle("Attrition") 
    
  })

[版本 2] 如果在闪亮的 ui 中来自 input$ 的输入列特征:需要使用 aes_string 而不是 aes_:

  output$cat_vs_cat_chart2 <- renderPlot({
    data() %>%
      ggplot(aes_string(x = input$cat_compare, group = "Attrition")) + 
      geom_bar(aes(y = ..prop.., fill = factor(..x..)), 
               stat="count", 
               alpha = 0.7) +
      geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), 
                stat= "count", 
                vjust = 2) +
      #labs(y = "Percentage", fill= "Education") +
      facet_grid(~Attrition) +
      theme_minimal()+
      theme(legend.position = "none", plot.title = element_text(hjust = 0.5)) + 
      ggtitle("Attrition") 
    
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2018-09-10
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多