R geom_bar and facet_grid labels on top of bars

前端 未结 1 1918
情歌与酒
情歌与酒 2021-01-24 03:27

I am trying to make this graph look better, and I\'m stuck with the labels (the numbers in this case). How can I make them to show on the top of their correspondent bar? Notice

相关标签:
1条回答
  • 2021-01-24 04:14

    You are almost there, just you need to move aes(fill=Gender) to inside ggplot

    library(tidyverse)
    #Reproducible data set
    test_mtcars <- mtcars %>% group_by(cyl,am, gear) %>% summarise(mean = mean(mpg))
    
    ggplot(test_mtcars, aes(as.factor(cyl), mean, fill=as.factor(am))) + geom_bar(stat = "identity", position = "dodge") + 
    facet_grid(~gear) + geom_text(aes(label = round(mean, 2)), position = position_dodge(width = 0.9), vjust = -1)
    

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