简体   繁体   English

ggplot显示堆叠时不计数的百分比

[英]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: 我使用以下代码来融化数据并使用NA删除行:

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." 当您访问条形图参考时,您可以阅读以下内容:“默认情况下,占据相同x位置的多个条形图将通过position_stack()彼此堆叠。(...)最后,position_fill()在每个位置都显示相对比例x通过堆叠钢筋,然后将每个钢筋标准化为具有相同的高度。”

Delete the position = "fill" argument and it should show a "count" legend instead of a percentage. 删除position = "fill"参数,它应该显示“ count”图例而不是百分比。

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. 能行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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