ggplot2()条形图和dplyr()在R中分组和整体数据

时间:2017-10-21 13:28:02

标签: r ggplot2

我想制作一个堆积比例条形图,表示居住在A,B和C镇的一群人中糖尿病的患病率。我还想要一个代表整个队列的条形图。

我对以下情节感到满意,但我想知道是否有办法将预处理步骤纳入处理步骤,即用dplyr()管道?

谢谢!

起点(df):

dfa <- data.frame(town=c("A","A","A","B","B","C","C","C","C","C"),diabetes=c("y","y","n","n","y","n","y","n","n","y"),heartdisease=c("n","y","y","n","y","y","n","n","n","y"))

预处理:

dfb <- rbind(dfa, transform(dfa, town = "ALL"))

处理和情节:

library(dplyr)
library(ggplot)

dfc <- dfb %>%
group_by(town) %>%
count(diabetes) %>%
mutate(prop = n / sum(n))

ggplot(dfc, aes(x = town, y = prop, fill = diabetes)) +
geom_bar(stat = "identity") +
coord_flip() 

1 个答案:

答案 0 :(得分:2)

像这样:

dfc <- dfa %>%
  bind_rows(dfa %>%
              mutate(town = "ALL")) %>%
  group_by(town) %>%
  count(diabetes) %>%
  mutate(prop = n / sum(n)) %>%
  ggplot(aes(x = town, y = prop, fill = diabetes)) +
    geom_bar(stat = "identity") +
    coord_flip() 

编辑:使用bind_rowsmutate代替rbindtransform

将预处理添加到管道中