I am using geom_histogram in R to produce a histogram using the code:
ggGender <- ggplot(dfGenderGrouped, aes(log(freq), fill=dfGenderGrouped$name) ) +
geom_histogram(data=dfGenderGrouped, binwidth = 1, alpha=0.5, color="black") + theme_bw() +
theme(axis.title = element_text(size=16), legend.text = element_text(size=12), axis.text.y = element_text(size=12, angle=45), axis.text.x = element_text(size=12), legend.position=c(0.8,0.7)) + ylab("Number of patients") +
xlab("Events (log)")+labs(fill="Events") + scale_y_continuous(labels = comma) +
scale_fill_brewer(palette="Spectral")
The dfGenderGrouped data frame looks like:
patid freq name Group
1 1156 1 Male - All events All
2 1194 1 Male - All events All
3 1299 1 Male - All events All
4 1445 1 Male - All events All
5 1476 2 Male - All events All
6 2045 2 Male - All events All
The unique values to name are presented in the legend. The unique values to Group are:
> unique(dfGenderGrouped$Group)
[1] "All" "Clinical" "Referral" "Therapy"
I would like to organise the stacks by the Group value e.g., in bin 0 you have a stacked column of Female - All events and Male - All events and then the same stacked column in binn 1 etc. For further clarification, I would then like Female - Clinical events and Male - Clinical events as a single stacked column also across the bins. Thus, each column of stacked values has the Group value in common (All, Clinical, Referral, and Therapy).
Further clarification, bin 0 would have the following column stacks (organised by Group in the data.frame):
Female - All events & Male - All events
Female - Clinical events & Male - Clinical events
Female - Referral events & Male - Referral events
Female - Therapy events & Male - Therapy events
Then for bin 1 the same:
Female - All events & Male - All events
Female - Clinical events & Male - Clinical events
Female - Referral events & Male - Referral events
Female - Therapy events & Male - Therapy events
Help is much appreciated.