[英]ggplot2() bar chart and dplyr() grouped and overall data in R
我想制作一个堆积的比例条形图,以表示居住在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()
像这样:
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_rows
和mutate
代替rbind
和transform
将预处理添加到管道中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.