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

时间:2016-10-24 05:46:17

标签: 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()

enter image description here

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

2 个答案:

答案 0 :(得分:28)

编辑:

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

这给出了:

enter image description here

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

这给出了:

enter image description here

这不一定是执行此操作的最常用方法,但您可以拥有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() 

以下是这样的:

enter image description here

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

以下是这样的:

enter image description here

答案 1 :(得分: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()