Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

ggplot2 - ggplot bar chart not show correctly on Shiny [got answers]

When I create bar chart below in R, there was no problem but when I convert these code to shiny function, the problem is occur, could you help ?

Bar chart in normal R code:

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

Bar chart convert to shiny code [error occur, not show facet_grid and text value like normal bar chart above] :

      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

question from:https://stackoverflow.com/questions/66058715/ggplot-bar-chart-not-show-correctly-on-shiny-got-answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

[Update] I got answers by myself:

[version 1] If input column feature directly to bar chart: need to add ~ to each column feature:

      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") 
    
  })

[version 2] If input column feature from input$ in shiny ui : need to use aes_string instead 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") 
    
  })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...