Center labels stacked bar (counts) ggplot2

后端 未结 2 1634
忘了有多久
忘了有多久 2020-12-31 12:13

I\'m attempting to place labels on a stacked bar plot using this approach (though if there\'s now a better approach I\'m open to what ever):

Showing data values on s

2条回答
  •  生来不讨喜
    2020-12-31 12:39

    I trying to do the same thing. So I tried your code with the same data and it doesn't, maybe there has been an update that changed something.

    Can someone check if the code still works?

    I tried using this code but doenter code hereesn't seem to work.

    dd <- ggplot_build(p)[[1]][[1]]
    ## Get the y-values in the middle of bars
    xy <- unique(dd[dd$y != 0, c("x", "y")])
    dat <- with(xy, data.frame(
           x=x,
           y=unlist(sapply(split(y, x), function(z) diff(c(0, z))/2 + head(c(0, z), -1)))
    ))
    
    ## Get the labels
    labels <- with(df[df$y!=0,], unlist(split(MaskID, x)))
    
    ## Add the text using the new xy-values and labels
    p + geom_text(data=dat, aes(x, y), label=labels, angle=90)
    

提交回复
热议问题