0

i am trying to create a stacked barchart showing the proportion difference between n TenYearCHD and prevalentHyp. As the difference is small and i trying to add a data label showing the numbers so it will be clearer. anyone know how to add the data label in my R script?

enter image description here

data_no_na %>% select(prevalentHyp, TenYearCHD) %>% filter(TenYearCHD == "Yes" ) %>% 
  ggplot() + 
  geom_bar(position='Fill', aes(x = TenYearCHD, fill=factor(prevalentHyp))) + 
  labs(x= "TenYearCHD", y = "Proportion", title = "prevalentHyp and TenYearCHD", fill="prevalentHyp") 
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • 3
    Does this answer your question? [Showing data values on stacked bar chart in ggplot2](https://stackoverflow.com/questions/6644997/showing-data-values-on-stacked-bar-chart-in-ggplot2) – UseR10085 Jun 23 '20 at 11:46
  • Dont really understand – Newbie coder Jun 23 '20 at 12:01
  • 1
    The answer provided in the question what I have referred solves your problem also. If you don't understand then copy and paste the output of `dput(data_no_na)` after editing the question to get an answer. Without your data, it is hard to answer. – UseR10085 Jun 23 '20 at 12:15
  • 1
    If you can't implement the solution provided by Bappa Das, please provide at least a sample of your data with `dput(data_no_na)` or if your data is very large `dput(data_no_na[1:20,])`. You can [edit] your question and paste the output. Please surround the output with three backticks (```) for better formatting. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell Jun 23 '20 at 12:17
  • 1
    Add `+ geom_text(size = 3, position = position_stack(vjust = 0.5))` to your code and see whether it solves your problem. – UseR10085 Jun 23 '20 at 12:18
  • 1
    I think you'd need `position="fill"` with `geom_text()` to match the `geom_bar()` layer, right @BappaDas? – chemdork123 Jun 23 '20 at 12:51

0 Answers0