R:带有百分比标签的多面条形图,每个图都独立

时间:2013-12-15 22:50:53

标签: r ggplot2

我正在尝试使用facet_grid生成几个图,其中每个图的百分比标签增加到100%。

在提供的图像中,百分比标签增加到49%(第一面)和51%(第二面)。

我见过this Question解决方案是在ggplot外聚合数据。我宁愿不这样做,我相信这是一种更好的方法。

library("ggplot2")
library("scales")

set.seed(123)

df <- data.frame(x = rnorm(10000, mean = 100, sd = 50))

df$factor_variable <- cut(df$x, right = TRUE, 
                          breaks = c(0, 25, 50, 100, 200, 10000),
                          labels = c("0 - 25", "26 - 50", "51 - 100", "101 - 200", "> 200")
                          )

df$second_factor_variable <- ifelse(df$x < 100, 1, 2)

df <- sample(df, x > 0)

table(df$second_factor_variable)

p1 <- ggplot(df, aes(x = factor_variable, y = (..count..)/sum(..count..), ymax = 0.8))
p1 <- p1 + geom_bar(fill = "deepskyblue3", width=.5)
p1 <- p1 + stat_bin(geom = "text",
                    aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
                    vjust = -1, color = "grey30", size = 6)
p1 <- p1 + xlab(NULL) + ylab(NULL)
p1 <- p1 + scale_y_continuous(label = percent_format())
p1 <- p1 + xlim("0 - 25", "26 - 50", "51 - 100", "101 - 200", "> 200")
p1 <- p1 + facet_grid(. ~ second_factor_variable)

print(p1)

Here is the attempt

1 个答案:

答案 0 :(得分:4)

此方法暂时有效。但是,PANEL变量没有记录,因此不应使用Hadley。 它似乎是聚合数据然后绘制的“正确”方式,在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