使用ggplot2 1.0.0,我按照下面的说明,找出如何绘制不同因素的百分比条形图:
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)
Run Code Online (Sandbox Code Playgroud)

但是,在使用时,我似乎无法获得总计数或每个条形图上方的百分比的标签geom_text.
上述代码的正确添加是什么,也保留了y轴的百分比?
Wal*_*ltS 46
留在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)
Run Code Online (Sandbox Code Playgroud)
对于计数,请在geom_bar和geom_text中将..density ..更改为..count ..
更新ggplot 2.x.
ggplot2 2.0进行了许多更改,ggplot包括在更改ggplot 2.0.0stat使用的默认函数时破坏了此代码的原始版本的更改.它不再像以前一样调用数据,而是调用每个位置的计数. 返回该位置的计数比例而不是. geom_bar stat_binstat_countstat_countpropdensity
下面的代码已经过修改,可以使用这个新版本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)
Run Code Online (Sandbox Code Playgroud)
第一个版本的情节如下所示.
eip*_*i10 15
如果您预先汇总数据,这会更容易.例如:
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)
Run Code Online (Sandbox Code Playgroud)
(仅供参考,你可以把标签栏里面也是这样,例如,通过改变代码来此的最后一行:y=pct*0.5), size=4, colour="white"))

我已经使用了所有代码并提出了这个问题.首先将你的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)
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
46422 次 |
| 最近记录: |