geom_text 如何根据需要在栏上放置文本?

geom_text how to position the text on bar as I want?

我想调整条形图上的文字。

我尝试调整 hjust/vjust 以显示我喜欢的内容,但它似乎无法正常工作。

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()

所以我希望数字位于 right-edge 中间的每个栏上,这样它就可以阅读,而不会像最后一部分那样重叠。

编辑:

hjust/vjust 表现得更聪明的更简单的解决方案是将 group 美学添加到 geom_text,然后 hjust & position 自动调整为 group

1。垂直方向

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()

这给出:

2。水平方向

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,取决于方向)变量。我不完全清楚如何 select 调整参数的值,目前它是基于 看起来 正确的。也许其他人可以建议一种更通用的选择此参数值的方法。

1。垂直方向

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() 

这是它的样子:

2。水平方向

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()

这是它的样子:

position_dodge() 语句采用宽度参数。为确保文本在条形末端居中(即,条形和文本的闪避宽度相同),为 geom_bar 中的 position_dodge() 语句提供相同的宽度参数并在 geom_text 内。

geom_bar还有一个宽度参数,即条形的宽度。如果您希望条形在每个 name 内相互对接,请使条形宽度与躲避宽度相同;如果你想在条之间有一个小的间隙,让条的宽度比闪避宽度小一点。

如果使用全局美学,则不需要 group 美学(但是,仅使用局部美学,则需要 geom_text 的群体美学)。

hjust = -0.5 会将文本标签放置在条形末端之外; hjust = 1.5 将它们放置在条形的末端。

library(ggplot2)

# Generate some data - using @tchakravarty's data - Thanks.
df = 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))

position = position_dodge(width = .75)
width = .75

ggplot(df, aes(x = name, y = count, label = count, fill = week)) + 
  geom_bar(width = width, stat='identity', position = position) +
  geom_text(hjust = -0.5, size = 2, position = position) +
  coord_flip() + 
  theme_bw()



# To separate the bars slightly:
position = position_dodge(width = .75)
width = .65

ggplot(df, aes(x = name, y = count, label = count, fill = week)) + 
  geom_bar(width = width, stat='identity', position = position) +
  geom_text(hjust = -0.5, size = 2, position = position) +
  coord_flip() + 
  theme_bw()