geom_text如何根据需要在文本上定位文本?

tmh*_*mhs 26 r ggplot2 geom-bar geom-text

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

我试图调整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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

所以我希望数字位于每个条形图上,中间位于右边缘,因此它的可读性不会像最后一部分那样重叠.

tch*_*rty 39

编辑:

该解决方案更容易获得hjust/ vjust的行为是明智的增加group美感geom_text,然后hjustposition调整为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()
Run Code Online (Sandbox Code Playgroud)

这给出了:

在此输入图像描述

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()
Run Code Online (Sandbox Code Playgroud)

这给出了:

在此输入图像描述


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

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() 
Run Code Online (Sandbox Code Playgroud)

这是看起来像:

在此输入图像描述

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()
Run Code Online (Sandbox Code Playgroud)

这是看起来像:

在此输入图像描述


San*_*att 6

position_dodge()语句采用宽度参数.要确保文本在条形的末端居中(即条形和文本的闪避宽度相同),请为position_dodge()语句内部geom_bar和内部提供相同的宽度参数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()
Run Code Online (Sandbox Code Playgroud)