How to label a barplot bar with positive and negative bars with ggplot2

前端 未结 1 825
不知归路
不知归路 2020-12-02 13:33

I\'m trying to plot a labeled barplot with ggplot2 with positive and negative bars.\"\" That works so far, but I would

相关标签:
1条回答
  • 2020-12-02 14:00

    This does the trick

    library(plyr)
    library(ggplot2)
    library(scales)
    dtf <- data.frame(x = c("ETB", "PMA", "PER", "KON", "TRA", 
                      "DDR", "BUM", "MAT", "HED", "EXP"),
                      y = c(.02, .11, -.01, -.03, -.03, .02, .1, -.01, -.02, 0.06))
    ggplot(dtf, aes(x, y)) +
      geom_bar(stat = "identity", aes(fill = x), legend = FALSE) + 
      geom_text(aes(label = paste(y * 100, "%"),
                   vjust = ifelse(y >= 0, 0, 1))) +
      scale_y_continuous("Anteil in Prozent", labels = percent_format()) +
      opts(axis.title.x = theme_blank())
    

    enter image description here

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