2017-09-26 161 views
1

assoicated分佈創建多因素geom_bar看看下面的示例數據:與相互

set.seed(123456) 
# 
Male_alive <- rbinom(100,1,0.6) 
Male_age <- sample(20:80, 100, T) 
# 
Female_alive <- rbinom(100,1,0.7) 
Female_age <- sample(20:80, 100, T) 
# 
Alive <- c(Male_alive, Female_alive) 
Age <- c(Male_age, Female_age) 
Sex <- c(rep('Male', length(Male_alive)),rep('Female', length(Female_alive))) 
# 
Patients <- data.frame(Alive, Age, Sex) 

我可以創建下面的代碼一個簡單的柱狀圖:

ggplot(Patients, aes(Sex, fill = factor(Alive))) + 
    geom_bar(position = "fill") 

但我想通過創建看似圖像(顏色不重要)的多因子條形圖(用於Sex and AgeGr)來擴展:

enter image description here

# 
Patients$AgeGr <- cut(Patients$Age, 6) 

使用

ggplot(Patients, aes(..., fill = factor(Alive))) + 
    geom_bar(position = "fill") + geom_wrap(~Sex) 

AgeGr只有填補高達Alive

+1

我想你會發現這個方法幫助:https://cran.r-project.org/web/packages/ggmosaic/vignettes/ggmosaic.html – Brian

回答

2

也許這可能是做一個可怕的方式:

#to get plots side by side 
library(gridExtra) 

#plot count of males 
pmales <-ggplot(Patients[Patients$Sex=='Male',], aes(Sex, fill =factor(Alive))) + geom_bar(position='fill') 

#plot grage males0 
pagegrmales0 <-ggplot(Patients[Patients$Sex=='Male' & Patients$Alive==0,], aes(Sex, fill =factor(AgeGr))) + geom_bar(position='fill') + ylab(NULL) +xlab(NULL) + theme(legend.position="none", plot.margin=unit(c(1,1,-0.5,1), "cm")) 

#plot grage males1 
pagegrmales1 <-ggplot(Patients[Patients$Sex=='Male' & Patients$Alive==1,], aes(Sex, fill =factor(AgeGr))) + geom_bar(position='fill') + ylab(NULL) +xlab(NULL) + theme(legend.position="none", plot.margin=unit(c(-0.5,1,1,1), "cm")) 

factorsmale <- grid.arrange(pagegrmales0, pagegrmales1, heights=c(prop.table(table(Patients[Patients$Sex=='Male',]$Alive))[[1]], prop.table(table(Patients[Patients$Sex=='Male',]$Alive))[[2]]), nrow=2) 

males <- grid.arrange(pmales, factorsmale, ncol =2, nrow= 2) 

######## 

#plot count of females 
pfemales <-ggplot(Patients[Patients$Sex=='Female',], aes(Sex, fill =factor(Alive))) + geom_bar(position='fill') 

#plot grage females0 
pagegrfemales0 <-ggplot(Patients[Patients$Sex=='Female' & Patients$Alive==0,], aes(Sex, fill =factor(AgeGr))) + geom_bar(position='fill') + ylab(NULL) +xlab(NULL) + theme(legend.position="none", plot.margin=unit(c(1,1,-0.5,1), "cm")) 

#plot grage females1 
pagegrfemales1 <-ggplot(Patients[Patients$Sex=='Female' & Patients$Alive==1,], aes(Sex, fill =factor(AgeGr))) + geom_bar(position='fill') + ylab(NULL) +xlab(NULL) + theme(legend.position="none", plot.margin=unit(c(-0.5,1,1,1), "cm")) 

factorsfemale <- grid.arrange(pagegrfemales0, pagegrfemales1, heights=c(prop.table(table(Patients[Patients$Sex=='Female',]$Alive))[[1]], prop.table(table(Patients[Patients$Sex=='Female',]$Alive))[[2]]), nrow=2) 

females <- grid.arrange(pfemales, factorsfemale, ncol =2, nrow= 2) 

grid.arrange(males, females, ncol = 2, nrow = 1) 

enter image description here

0

首先相應的高度調節,你需要創建長格式的數據集只需要列,然後劇情如下圖所示:

library("reshape2") 
mPatients <- melt(Patients[,-2], id.vars = "Sex") 

ggplot(mPatients, aes(x=variable, fill = factor(value))) + 
    geom_bar(position = "fill") + facet_wrap(~Sex) 

enter image description here

+0

感謝試圖幫助,但AgeGr應在每個方面重複兩次。一次爲Alive == 1,然後再爲Alive == 0。也就是說,第一套Male-AgeGr應該只上升到0.7 – user08041991

+0

這是否是在這個問題的要求?如果不是,請編輯或提出一個新問題。 – Prradep

+0

當看到所需的圖像 - 對於男性 - 活着 - 綠色部分貢獻約50%和紅色%。然後在這50%的部分中,AgeGr的分佈被繪製爲每個 – user08041991

1

合併活着,agegroup列

Patients$Alive_AgeGr <- paste(Patients$Alive, Patients$AgeGr, sep="_") 

情節

ggplot(Patients, aes(x = factor(Alive), fill = factor(AgeGr))) + 
    geom_bar(position = "fill") + facet_wrap(~Sex) 

plot of alive and age group

+0

我相信這不是OP正在尋找的行爲 – erasmortg

+0

@erasmortg,我在你的答案中看到的情節是不同的。 OP真的想重現他在問題中提供的情節。我沒有理解AgeGrp在原始情節中被Alive分開。 –