R: Faceted bar chart with percentages labels independent for each plot

后端 未结 1 641
小蘑菇
小蘑菇 2020-12-17 03:24

I\'m trying to use facet_grid to produce several plots where each plot\'s percentage labels add to 100%.

In the image provided, the percentages labels add to 49% (fi

相关标签:
1条回答
  • 2020-12-17 03:38

    This method for the time being works. However the PANEL variable isn't documented and according to Hadley shouldn't be used. It seems the "correct" way it to aggregate the data and then plotting, there are many examples of this in SO.

    ggplot(df, aes(x = factor_variable, y = (..count..)/ sapply(PANEL, FUN=function(x) sum(count[PANEL == x])))) +
                     geom_bar(fill = "deepskyblue3", width=.5) +
                     stat_bin(geom = "text",
                              aes(label = paste(round((..count..)/ sapply(PANEL, FUN=function(x) sum(count[PANEL == x])) * 100), "%")),
                              vjust = -1, color = "grey30", size = 6) +
                     facet_grid(. ~ second_factor_variable)
    

    enter image description here

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