需要在ggplot2中绘制条形图(以百分比方式) [英] Need to Draw a Bar Graph ( in percentile manner ) in ggplot2
本文介绍了需要在ggplot2中绘制条形图(以百分比方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我有这样的数据集
所有严重错误警告复查
2016 1412 475 4 125
154 45 49 2 58
116 86 12 1 17
我想使用ggplot2来绘制一个堆叠的条形图,其中单个栏将显示100%的ALL,其余的Critical,Error,Warning,Review应该根据他们的贡献所有。
我尝试过没有运气!需要手......感谢
解决方案
我不太确定您对所需图表的描述是否不含糊。 / p>
我的解释如下:
##从user1317221_G复制- 感谢那。
babydf < - 结构(列表(ALL = c(2016L,154L,116L),Critical = c(1412L,
45L,86L),错误= c(475L,49L,12L) = c(4L,2L,1L),
Review = c(125L,58L,17L)),.Names = c(ALL,Critical,
Error,Warning ,Review),class =data.frame,row.names = c(NA,
-3L))
#添加ID
babydf< - cbind (b = 1:nrow(babydf),babydf))
library(reshape2)
library(ggplot2)
#重塑数据框:
df.reshaped < - melt(babydf,id.vars ='id')
ggplot(子集(df.reshaped,variable!='ALL'),aes(x = id,y = value,fill = variable))+ geom_bar(stat ='identity')
如果你想让所有等高的酒吧,只要做到
babydf [,3:6]< - babydf [,3:6] / babydf $ ALL * 100
在 melt
之前。结果:
hi i have a data set like this
ALL Critical Error Warning Review
2016 1412 475 4 125
154 45 49 2 58
116 86 12 1 17
I want to plot a stacked bar graph using ggplot2 where a single bar would show 100% of "ALL" and rest "Critical","Error","Warning","Review" should be on top of another according to their contribution in "ALL".
I am try it with no luck!!! Need a hand..Thanks
解决方案
I'm not quite sure if your description of the desired plot is non-ambiguous.
My interpretation would be the following:
## Copied from user1317221_G - Thanks for that.
babydf <- structure(list(ALL = c(2016L, 154L, 116L), Critical = c(1412L,
45L, 86L), Error = c(475L, 49L, 12L), Warning = c(4L, 2L, 1L),
Review = c(125L, 58L, 17L)), .Names = c("ALL", "Critical",
"Error", "Warning", "Review"), class = "data.frame", row.names = c(NA,
-3L))
# Add IDs
babydf <- cbind(id=1:nrow(babydf), babydf))
library(reshape2)
library(ggplot2)
# reshape the dataframe:
df.reshaped <- melt(babydf, id.vars='id')
ggplot(subset(df.reshaped, variable != 'ALL'), aes(x=id, y=value, fill=variable)) + geom_bar(stat='identity')
If you want to have all bars of equal height, just do
babydf[, 3:6] <- babydf[, 3:6] / babydf$ALL * 100
before melt
. The result:
这篇关于需要在ggplot2中绘制条形图(以百分比方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文