简体   繁体   中英

ggplot showing percent not count when stacking

I am trying make a stacked barplot of following data:

 df_APP -> Date        CBPP3  ABSPP    PSPP   CSPP
           2018-06-01 254551  27413 1991168 157034
           2018-05-25 253297  27241 1987753 155648
           2018-05-18 253759  27428 1984125 154796
           2018-05-11 253270  27149 1980743 153637
           2018-05-04 252583  27135 1972850 152593

I use following code to melt the data and delete rows with NAs:

APP <- as.data.frame(df_APP)
new_APP <- melt(APP, id = "Date")
new_APP <- new_APP[-which(is.na(new_APP$value)),]

I plot the melted dataset using:

ggplot(new_APP, aes(x=Date, y=value, fill = variable)) +
geom_bar(position = "fill", stat = "identity") 

My graph does not show count but percent instead as you can see below, and I cannot figure out why.

在此处输入图片说明

When you visit the bar charts reference you can read the following:"By default, multiple bars occupying the same x position will be stacked atop one another by position_stack(). (...) Finally, position_fill() shows relative proportions at each x by stacking the bars and then standardising each bar to have the same height."

Delete the position = "fill" argument and it should show a "count" legend instead of a percentage.

Position fill will fill up the full chart across every point to show the ratio. Hence the "fill" that exists. That is a stacked bar chart that you have posted

ggplot(new_APP, aes(x=Date, y=value, fill = variable)) + geom_col()

can work, also

ggplot(new_APP, aes(x=Date, y=value, fill = variable)) +
geom_bar(position = "stack", stat = "identity") 

can work.

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