0

this might be a simple question. I have a big df of clinical data called d.

     head(d[,c("catCRP","ageCat")])
      catCRP  ageCat
    1     <4 (50,66]
    2     >4 (40,50]
    3     >4 (30,40]
    4     <4 (50,66]
    5     >4 (30,40]
    6     >4 (30,40]

I am plotting it with ggplot2:

ggplot(d,aes(x=ageCat,fill=catCRP))+geom_bar(position="fill")

In this case, ggplot draws the percentages in each category.

Is it possible to make it add the percentage numbers AND the actual count to the plot?

I'm a beginner, I have looked in all the documentation but I have not found the answer.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Emos
  • 43
  • 7
  • See `geom_text` or `geom_label`, maybe. – alistaire Oct 01 '16 at 17:48
  • Discretizing data often distorts or obscures the finer points of relationships between continuous variables. You might also want to try: `ggplot(d, aes(CRP, group=ageCat)+geom_density()` or `ggplot(d, aes(y=CRP, x=age)+geom_density_2d()` – IRTFM Oct 01 '16 at 17:54

1 Answers1

0
library(ggplot2)
library(dplyr)

d <- data.frame(ageCat = rep(1:5, each=20)
                ,catCRP = rep(1:5,20))

p <- d %>% group_by(ageCat, catCRP) %>% summarise(n = n()) %>%
 mutate(p = n/sum(n))

p1 <- ggplot(p,aes(x=ageCat,y=p, fill=catCRP))+geom_bar(stat = "identity", position="fill") + theme_bw() 
p1 + geom_text(aes(label = paste0(n," (",p*100,"%)")), size = 3, hjust = 0.5, vjust = 3, position =     "stack") 
Wietze314
  • 5,942
  • 2
  • 21
  • 40
  • To ensure that the y axis is depicted as a percentage use the `scale` package and then append `scale_y_continuous(label = percent)` – Jacob H Oct 01 '16 at 18:23
  • Instead of using `vjust` to position the labels, add `ypos = cumsum(p) - 0.5*p)` to your `mutate` statement, and then use `y=ypos` to position the labels in `geom_text`. That way, you'll get exact vertical centering of the labels within each bar section. Also, in the typical situation where percentages are not integers, it's a good idea to include `round` or `sprintf` to get the number of decimal places you prefer for the percentages. For an example of these suggestions, see [this SO answer](http://stackoverflow.com/a/37818965/496488). – eipi10 Oct 01 '16 at 20:02