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

时间:2022-11-16 23:39:57

I'd like to make a stacked proportional bar chart representing the prevalence of diabetes in a cohort of individuals residing in towns A, B, and C. I'd also like the plot to feature a bar representing the entire cohort.

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

I'm happy with the below plot, but I'd like to know if there is a way of incorporating the pre-processing step into the processing step, ie piping it with dplyr()?

我很满意下面的情节,但我想知道是否有办法将预处理步骤纳入处理步骤,即用dplyr()管道它?

Thanks!

谢谢!

Starting point (df):

起点(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"))

Pre-processing:

前处理:

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

Processing and plot:

处理和情节:

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 个解决方案

#1


2  

Like this:

喜欢这个:

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() 

EDIT: added pre-processing into pipeline using bind_rows and mutate instead of rbind and transform

编辑:使用bind_rows和mutate而不是rbind和transform将预处理添加到管道中

#1


2  

Like this:

喜欢这个:

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() 

EDIT: added pre-processing into pipeline using bind_rows and mutate instead of rbind and transform

编辑:使用bind_rows和mutate而不是rbind和transform将预处理添加到管道中