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追加してから、自動的に調整することです。groupgeom_texthjustpositiongroup

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()ステートメントは幅パラメーターを取ります。テキストが棒の端の中央に配置されるようにするには (つまり、棒とテキストの覆い焼き幅が同じになるようにするため)、 withinと withinのposition_dodge()ステートメントに同じ幅パラメータを指定します。geom_bargeom_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 に答える