如何在百分比条形图上方添加百分比或计数标签?
How to add percentage or count labels above percentage bar plot?
使用 ggplot2 1.0.0
,我按照下面的说明 post 弄清楚如何绘制跨因子的百分比条形图:
Sum percentages for each facet - respect "fill"
test <- data.frame(
test1 = sample(letters[1:2], 100, replace = TRUE),
test2 = sample(letters[3:8], 100, replace = TRUE)
)
library(ggplot2)
library(scales)
ggplot(test, aes(x= test2, group = test1)) +
geom_bar(aes(y = ..density.., fill = factor(..x..))) +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
但是,在使用 geom_text
时,我似乎无法获得总计数或每个条形图上方百分比的标签。
在上面的代码中添加的正确内容是什么同时保留了百分比 y 轴?
如果您预先汇总数据,这样做会更容易。例如:
library(ggplot2)
library(scales)
library(dplyr)
set.seed(25)
test <- data.frame(
test1 = sample(letters[1:2], 100, replace = TRUE),
test2 = sample(letters[3:8], 100, replace = TRUE)
)
# Summarize to get counts and percentages
test.pct = test %>% group_by(test1, test2) %>%
summarise(count=n()) %>%
mutate(pct=count/sum(count))
ggplot(test.pct, aes(x=test2, y=pct, colour=test2, fill=test2)) +
geom_bar(stat="identity") +
facet_grid(. ~ test1) +
scale_y_continuous(labels=percent, limits=c(0,0.27)) +
geom_text(data=test.pct, aes(label=paste0(round(pct*100,1),"%"),
y=pct+0.012), size=4)
(仅供参考,您也可以将标签放在栏内,例如,将最后一行代码更改为:y=pct*0.5), size=4, colour="white")
)
留在ggplot内,你可以试试
ggplot(test, aes(x= test2, group=test1)) +
geom_bar(aes(y = ..density.., fill = factor(..x..))) +
geom_text(aes( label = format(100*..density.., digits=2, drop0trailing=TRUE),
y= ..density.. ), stat= "bin", vjust = -.5) +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
对于计数,在 geom_bar 和 geom_text
中将 ..density.. 更改为 ..count..
更新 ggplot 2.x
ggplot2 2.0
对 ggplot
进行了许多更改,包括更改 geom_bar
[=22] 使用的默认 stat
函数时破坏了此代码的原始版本=].它不像以前那样调用 stat_bin
来对数据进行分箱,而是现在调用 stat_count
来计算每个位置的观测值。 stat_count
returns prop
作为该位置的计数比例,而不是 density
。
下面的代码已经过修改,可以与 ggplot2
的这个新版本一起使用。我已经包括了两个版本,这两个版本都显示了条形高度占计数的百分比。第一个以百分比形式显示柱上方的计数比例,而第二个显示柱上方的计数。我还为 y 轴和图例添加了标签。
library(ggplot2)
library(scales)
#
# Displays bar heights as percents with percentages above bars
#
ggplot(test, aes(x= test2, group=test1)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5) +
labs(y = "Percent", fill="test2") +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
#
# Displays bar heights as percents with counts above bars
#
ggplot(test, aes(x= test2, group=test1)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes(label = ..count.., y= ..prop..), stat= "count", vjust = -.5) +
labs(y = "Percent", fill="test2") +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
第一个版本的情节如下所示。
我已经使用了您的所有代码并想出了这个。首先将你的 ggplot 分配给一个变量,即 p <- ggplot(...) + geom_bar(...) 等。然后你可以这样做。你不需要总结太多,因为 ggplot 有一个构建函数,它已经为你提供了所有这些。我会留给你格式化等。祝你好运。
dat <- ggplot_build(p)$data %>% ldply() %>% select(group,density) %>%
do(data.frame(xval = rep(1:6, times = 2),test1 = mapvalues(.$group, from = c(1,2), to = c("a","b")), density = .$density))
p + geom_text(data=dat, aes(x = xval, y = (density + .02), label = percent(density)), colour="black", size = 3)
使用 ggplot2 1.0.0
,我按照下面的说明 post 弄清楚如何绘制跨因子的百分比条形图:
Sum percentages for each facet - respect "fill"
test <- data.frame(
test1 = sample(letters[1:2], 100, replace = TRUE),
test2 = sample(letters[3:8], 100, replace = TRUE)
)
library(ggplot2)
library(scales)
ggplot(test, aes(x= test2, group = test1)) +
geom_bar(aes(y = ..density.., fill = factor(..x..))) +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
但是,在使用 geom_text
时,我似乎无法获得总计数或每个条形图上方百分比的标签。
在上面的代码中添加的正确内容是什么同时保留了百分比 y 轴?
如果您预先汇总数据,这样做会更容易。例如:
library(ggplot2)
library(scales)
library(dplyr)
set.seed(25)
test <- data.frame(
test1 = sample(letters[1:2], 100, replace = TRUE),
test2 = sample(letters[3:8], 100, replace = TRUE)
)
# Summarize to get counts and percentages
test.pct = test %>% group_by(test1, test2) %>%
summarise(count=n()) %>%
mutate(pct=count/sum(count))
ggplot(test.pct, aes(x=test2, y=pct, colour=test2, fill=test2)) +
geom_bar(stat="identity") +
facet_grid(. ~ test1) +
scale_y_continuous(labels=percent, limits=c(0,0.27)) +
geom_text(data=test.pct, aes(label=paste0(round(pct*100,1),"%"),
y=pct+0.012), size=4)
(仅供参考,您也可以将标签放在栏内,例如,将最后一行代码更改为:y=pct*0.5), size=4, colour="white")
)
留在ggplot内,你可以试试
ggplot(test, aes(x= test2, group=test1)) +
geom_bar(aes(y = ..density.., fill = factor(..x..))) +
geom_text(aes( label = format(100*..density.., digits=2, drop0trailing=TRUE),
y= ..density.. ), stat= "bin", vjust = -.5) +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
对于计数,在 geom_bar 和 geom_text
中将 ..density.. 更改为 ..count..更新 ggplot 2.x
ggplot2 2.0
对 ggplot
进行了许多更改,包括更改 geom_bar
[=22] 使用的默认 stat
函数时破坏了此代码的原始版本=].它不像以前那样调用 stat_bin
来对数据进行分箱,而是现在调用 stat_count
来计算每个位置的观测值。 stat_count
returns prop
作为该位置的计数比例,而不是 density
。
下面的代码已经过修改,可以与 ggplot2
的这个新版本一起使用。我已经包括了两个版本,这两个版本都显示了条形高度占计数的百分比。第一个以百分比形式显示柱上方的计数比例,而第二个显示柱上方的计数。我还为 y 轴和图例添加了标签。
library(ggplot2)
library(scales)
#
# Displays bar heights as percents with percentages above bars
#
ggplot(test, aes(x= test2, group=test1)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5) +
labs(y = "Percent", fill="test2") +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
#
# Displays bar heights as percents with counts above bars
#
ggplot(test, aes(x= test2, group=test1)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes(label = ..count.., y= ..prop..), stat= "count", vjust = -.5) +
labs(y = "Percent", fill="test2") +
facet_grid(~test1) +
scale_y_continuous(labels=percent)
第一个版本的情节如下所示。
我已经使用了您的所有代码并想出了这个。首先将你的 ggplot 分配给一个变量,即 p <- ggplot(...) + geom_bar(...) 等。然后你可以这样做。你不需要总结太多,因为 ggplot 有一个构建函数,它已经为你提供了所有这些。我会留给你格式化等。祝你好运。
dat <- ggplot_build(p)$data %>% ldply() %>% select(group,density) %>%
do(data.frame(xval = rep(1:6, times = 2),test1 = mapvalues(.$group, from = c(1,2), to = c("a","b")), density = .$density))
p + geom_text(data=dat, aes(x = xval, y = (density + .02), label = percent(density)), colour="black", size = 3)