41

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

我试图调整 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()

在此处输入图像描述

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

4

2 回答 2

69

编辑:

获得hjust/vjust表现智能的更简单的解决方案是添加group美学,geom_text然后自动调整。hjustpositiongroup

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

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

看起来是这样的:

在此处输入图像描述

于 2016-10-24T07:16:25.850 回答
12

position_dodge()语句采用宽度参数。为确保文本在条的末端居中(即条的闪避宽度与文本相同),为position_dodge()语句 withingeom_bar和 within赋予相同的宽度参数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()
于 2016-10-25T00:58:17.017 回答