geom_text不标记躲避的geom_bar [英] geom_text not labelling dodged geom_bar

查看:66
本文介绍了geom_text不标记躲避的geom_bar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法通过 CLASS (图被躲避"的因素)来使geom_label标记已躲避的条形图.相反,我正在获取每个 PROC ( Y 轴)的总 count :

I can't seem to get geom_label to label a dodged bar plot by CLASS (the factor by which the plot is "dodged"). Rather, I am getting the total count per PROC (the Y axis):

ggplot(data = df, mapping = aes(x = PROC)) +
geom_bar(mapping = aes(fill = CLASS), position = "dodge") +
geom_text(stat = "count", aes(x = PROC, label = ..count..)) +
theme(axis.title.y = element_blank(),
    axis.title.x = element_blank(),
    axis.ticks.y = element_blank(),
    axis.ticks.x = element_blank(),
    axis.text.x =  element_blank()) + 
scale_x_discrete(labels = function(x) str_wrap( 
    PROC.Labels, 
    width = 10)) +
coord_flip() 

此外,我不知道为什么105 geom_text 标签出现在此条形图的右侧.

Additionally, I don't know why the 105 geom_text label is appearing so far to the right of this bar plot.

推荐答案

您需要更新 geom_text 以使用 position_dodge()函数.这是一个使用内置菱形数据集的示例,与您的示例非常相似.我还使用了 ggplot 3.0的 stat()函数,而不是已弃用的 .count .. 变量.

You need to update geom_text to use the position_dodge() function. Here's an example, very similar to yours, using the built-in diamonds data set. I'm also using ggplot 3.0's stat() function, rather than the deprecated ..count.. variable.

您的标签显示在最右边,因为它们代表每个组的总数计数,因此位于相应的较高y位置.

Your labels appear far to the right because they represent the total count for each of your groups, and are thus placed at the corresponding higher (farther right) y position.

请注意,提供 position_dodge()的宽度值为0.9对应于以下事实:默认情况下,分类条形图(或一组躲避的条形图)占用了其90%的可用空间.轴,其余的10%会移至条形分组之间的边距.

Note that providing position_dodge() a width value of 0.9 corresponds to the fact that by default, a categorical bar (or a dodged group of bars) takes up 90% of its available space on the axis, with the remaining 10% going to the margin between the bar groupings.

g <- ggplot(data = diamonds, aes(x = cut, fill = color)) +
  geom_bar(position = 'dodge') +
  geom_text(stat = 'count', hjust = 0, position = position_dodge(0.9), aes(x = cut, label = stat(count))) +
  coord_flip()
print(g)

这篇关于geom_text不标记躲避的geom_bar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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