geom_text 如何根据需要在栏上定位文本? [英] geom_text how to position the text on bar as I want?

查看:44
本文介绍了geom_text 如何根据需要在栏上定位文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调整条形图上的文本.

I would like to adjust the text on the barplot.

我尝试将 hjust/vjust 调整为我喜欢的显示方式,但似乎无法正常工作.

I tried to adjust hjust/vjust to display as I like it but it seems like it's not working properly.

ggplot(data) + 
        geom_bar(aes(name, count, 
        fill = week), stat='identity', position = 'dodge') +
        geom_text(aes(name,count, 
        label=count),hjust=0.5, vjust=3, size=2,
        position = position_dodge(width = 1)) + 
        coord_flip()

所以我希望数字位于每个条形的中间、右边缘,这样它就可以阅读而不会像最后一部分一样重叠.

So I would like the numbers to situate on each bar, in the middle, at the right-edge so it's readable without overlapping like the last parts.

推荐答案

hjust/vjust 智能表现的更简单的解决方案是将 group 美学添加到 geom_text然后 hjust &position 自动调整group.

The easier solution to get hjust/vjust to behave intelligently is to add the group aesthetic to geom_text and then hjust & position adjust for the group automatically.

ggplot(data) + 
  geom_bar(
    aes(x = name, y = count, fill = week, group = week), 
    stat='identity', position = 'dodge'
  ) +
  geom_text(
    aes(x = name, y = count, label = count, group = week),
    position = position_dodge(width = 1),
    vjust = -0.5, size = 2
  ) + 
  theme_bw()

这给出:

ggplot(data) + 
  geom_bar(
    aes(x = name, y = count, fill = week, group = week), 
    stat='identity', position = 'dodge'
  ) +
  geom_text(
    aes(x = name, y = count, label = count, group = week), 
    hjust = -0.5, size = 2,
    position = position_dodge(width = 1),
    inherit.aes = TRUE
  ) + 
  coord_flip() + 
  theme_bw()

这给出:

这不一定是最通用的方法,但你可以有一个 fill 依赖 hjust(或 vjust,取决于方向)变量.我不太清楚如何选择调整参数的值,目前它是根据看起来正确的.也许其他人可以建议一种更通用的选择此参数值的方法.

This is not necessarily the most general way to do this, but you can have a fill dependent hjust (or vjust, depending on the orientation) variable. It is not entirely clear to me how to select the value of the adjustment parameter, and currently it is based on what looks right. Perhaps someone else can suggest a more general way of picking this parameter value.

library(dplyr)
library(ggplot2)

# generate some data
data = data_frame(
  week = as.factor(rep(c(1, 2), times = 5)),
  name = as.factor(rep(LETTERS[1:5], times = 2)),
  count = rpois(n = 10, lambda = 20),
  hjust = if_else(week == 1, 5, -5),
  vjust = if_else(week == 1, 3.5, -3.5)
)

# Horizontal
ggplot(data) + 
  geom_bar(
    aes(x = name, y = count, fill = week, group = week), 
    stat='identity', position = 'dodge'
  ) +
  geom_text(
    aes(x = name, y = count, label = count, vjust = vjust), 
    hjust = -0.5, size = 2,
    inherit.aes = TRUE
  ) + 
  coord_flip() + 
  theme_bw() 

这是它的样子:

ggplot(data) + 
  geom_bar(
    aes(x = name, y = count, fill = week, group = week), 
    stat='identity', position = 'dodge'
  ) +
  geom_text(
    aes(x = name, y = count, label = count, vjust = vjust), 
    hjust = -0.5, size = 2,
    inherit.aes = TRUE
  ) + 
  coord_flip() + 
  theme_bw()

这是它的样子:

这篇关于geom_text 如何根据需要在栏上定位文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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