如何在百分比条形图上添加百分比或计数标签? [英] How to add percentage or count labels above percentage bar plot?

查看:1094
本文介绍了如何在百分比条形图上添加百分比或计数标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ggplot2 1.0.0 ,我按照下面的帖子中的说明找出了如何绘制跨因子的百分比条形图:

每个方面的总和百分比 - 尊重填充 ;

  test<  -  data.frame(
test1 = sample(letters [1:2 ],100,replace = TRUE),
test2 = sample(字母[3:8],100,replace = TRUE)

library(ggplot2)
library )
ggplot(test,aes(x = test2,group = test1))+
geom_bar(aes(y = ..density ..,fill = factor(.. x ..)))+
facet_grid(〜test1)+
scale_y_continuous(labels = percent)

< img src =https://i.stack.imgur.com/siicZ.pngalt =然而,我似乎无法当使用 geo时,获得总计数或每个柱状图上方百分比的标签m_text



以上代码的正确补充也保留了y轴的百分比?

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)

..density .. to ..count .. in geom_bar and geom_text



ggplot 2.x的更新



ggplot2 2.0 ggplot 进行了许多更改,包括打破此原始版本的更改代码改变了由 geom_bar stat 函数。组织/ 2015/12 / 21 / ggplot2-2-0-0 /> ggplot 2.0.0 。与前面一样,不是像以前那样调用 stat_bin 来对数据进行分类,而是调用 stat_count 来计算每个位置的观察值。 stat_count 返回 prop 作为该位置的计数比例,而不是密度

下面的代码已被修改,可用于这个新版本的 ggplot2 。我已经包含了两个版本,这两个版本都以条数的百分比显示了条的高度。第一个显示比例高于酒吧的百分比,第二个显示高于酒吧的计数。我还为y轴和图例添加了标签。

  library(ggplot2)
library(scales)

#以高于百分比的百分比显示酒吧高度酒吧

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)

#将bar高度显示为高于bar的百分数

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)

从第一个版本开始的情节如下所示:


Using ggplot2 1.0.0, I followed the instructions in below post to figure out how to plot percentage bar plots across factors:

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)

However, I cannot seem to get a label for either the total count or the percentage above each of the bar plots when using geom_text.

What is the correct addition to the above code that also preserves the percentage y-axis?

解决方案

Staying within ggplot, you might try

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)

For counts, change ..density.. to ..count.. in geom_bar and geom_text

UPDATE for ggplot 2.x

ggplot2 2.0 made many changes to ggplot including one that broke the original version of this code when it changed the default stat function used by geom_bar ggplot 2.0.0. Instead of calling stat_bin, as before, to bin the data, it now calls stat_count to count observations at each location. stat_count returns prop as the proportion of the counts at that location rather than density.

The code below has been modified to work with this new release of ggplot2. I've included two versions, both of which show the height of the bars as a percentage of counts. The first displays the proportion of the count above the bar as a percent while the second shows the count above the bar. I've also added labels for the y axis and legend.

  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)

The plot from the first version is shown below.

这篇关于如何在百分比条形图上添加百分比或计数标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆