When i have this code:
C_df <- as.data.frame(C)
C_df <- C_df %>%
group_by(core) %>%
mutate(percent_weight = paste0((round(Freq / sum(Freq),2) * 100),"%"))
ggplot(C_df, aes(fill=condition, y=Freq, x=core)) +
geom_bar(position="fill", stat="identity")+
theme_classic()+
scale_y_continuous(expand = c(0, 0),labels = scales::percent, labs(y="Proportion of Cohort (%)"))+
geom_col(colour = "black", position = "fill")+
scale_fill_brewer(palette = "Pastel1")
I can produce this graph:
However when i add this one line trying to put the percentages into the bars:
C_df <- as.data.frame(C)
C_df <- C_df %>%
group_by(core) %>%
mutate(percent_weight = paste0((round(Freq / sum(Freq),2) * 100),"%"))
ggplot(C_df, aes(fill=condition, y=Freq, x=core)) +
geom_bar(position="fill", stat="identity")+
theme_classic()+
scale_y_continuous(expand = c(0, 0),labels = scales::percent, labs(y="Proportion of Cohort (%)"))+
geom_col(colour = "black", position = "fill")+
scale_fill_brewer(palette = "Pastel1")+
geom_text(aes(label = percent_weight),colour = "white", size = 3, position = position_dodge(.9))
I get this graph:
And i cant for the life of me figure out why
Dummy Data:
| Core | Condition | Freq | percent_weight |
|---|---|---|---|
| Core | TRUE | 8 | 8% |
| non-Core | TRUE | 8 | 8% |
| Postcode Core | TRUE | 6 | 6% |
| Postcode non | TRUE | 8 | 8% |
| Core | FALSE | 92 | 92% |
| non-Core | FALSE | 92 | 92% |
| Postcode Core | FALSE | 94 | 94% |
| Postcode non | FALSE | 92 | 92% |
Data:


