带有分组数据的R条形图(小节)

时间:2019-12-06 21:31:00

标签: r dplyr

是否有一种快速简便的方法来从这种类型的输出生成条形图?不确定是否可以在不重塑数据或单独的dplyr语句的情况下做到这一点。

res <-
  df %>%
  mutate(categoryName = factor(categoryName, levels = c('Not Applicable', 'Very Poor', 'Bad', 'OK', 'Good', 'Very Good'))) %>%
  group_by(categoryName) %>%
  count() %>%
  adorn_totals('row') %>%
  spread(categoryName, n) %>%
  mutate(Yes = `OK` + `Good` + `Very Good`)

res

输出看起来像这样(为了保密起见,我编辑了小数位数,但都是正整数)。我不会获取“是”或“总计”列(需要使用(是/总计)%的计算)。

enter image description here

1 个答案:

答案 0 :(得分:1)

这应该对您有用:

res %>% 
  gather("Rating",'Instances') %>% 
  filter(Rating %in% c('Not Applicable', 'Very Poor', 'Bad', 'OK', 'Good', 'Very Good')) %>%
  ggplot(aes(x=Rating,y=Instances)) + 
  geom_bar(stat='identity')