Add legend to “geom_bar” using the ggplot2 package

前端 未结 2 464
别跟我提以往
别跟我提以往 2020-12-10 03:41

I am a newbie to R, so please pardon my ignorance. I made a pseudo-stacked barplot in which I drew 4 sets of bars on top of each other using geom_bar. There are 4 health sta

2条回答
  •  失恋的感觉
    2020-12-10 04:22

    You need to reshape your data.

    library(reshape)
    library(ggplot2)
    
    x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), alive = c(627,208,109),  infected = c(102,27,0), dead = c(133,112,12), sod.dead=c(49,8,0)))
    
    x <- melt(x)
    colnames(x) <- c("Type","Status","value")
    
    ggplot(x, aes(Type, value, fill=Status)) + geom_bar(position="stack")
    

提交回复
热议问题