简体   繁体   中英

proportional bar plot with ggplot2

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

在此处输入图片说明

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?

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM