0

I have a dataframe d

> head(d,10)
     age3 num_sint
1091   25        2
835    38        0
993    38        1
1480   38        0
1996   38        0
2216   38        0
3126   38        0
3931   38        0
6479   38        0
6784   38        0

Where num_sint is a variable between 0 and 8. I would like to plot something like this

enter image description here

With age3 on the x axis and on the y axis the proportion.

All the columns should go from 0 to 1

The column for age3=25 would be all red (because 100%=2). The column for age3=38 would be something like 10% blue and 90% green.

I spent a lot of time on this. Is there any easy solution?

Community
  • 1
  • 1
Donbeo
  • 17,067
  • 37
  • 114
  • 188

1 Answers1

3

You might be looking for something like this:

ggplot(d, aes(x = as.factor(age3), fill = num_sint)) + 
    geom_bar(position = "fill") + 
    scale_y_continuous(labels = percent_format())
Jaap
  • 81,064
  • 34
  • 182
  • 193