Need to Draw a Bar Graph ( in percentile manner ) in ggplot2

后端 未结 1 1919
鱼传尺愫
鱼传尺愫 2021-01-29 15:16

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                


        
相关标签:
1条回答
  • 2021-01-29 16:00

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

    enter image description here

    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: enter image description here

    0 讨论(0)
提交回复
热议问题