始める無料で始める

ラベルの位置のずれを修正する

ドットプロットのラベルが、まだ線と少し重なっています。条件付きの hjust 属性を使ってラベルの位置を調整し、見た目を整えましょう。

この演習はコースの一部です

Tidyverseによるデータコミュニケーション

コースを見る

演習の手順

  • geom_text() 関数の hjust 属性に、年が "2006" のラベルの場合は 1.4、それ以外の場合は -0.4 を設定します。ifelse() 関数を使いましょう。
  • 同じく geom_text() 呼び出しの中で、フォントサイズ、フォントファミリー、カラーをそれぞれ 3"Bookman""gray25" に変更します。これらの値はデータに依存しないため、aes() 関数の外で指定してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Save plot into an object for reuse
ilo_dot_plot <- ggplot(ilo_data) +
  geom_path(aes(x = working_hours, y = country),
            arrow = arrow(length = unit(1.5, "mm"), type = "closed")) +
    # Specify the hjust aesthetic with a conditional value
    geom_text(
          aes(x = working_hours,
              y = country,
              label = round(working_hours, 1),
              hjust = ifelse(___)
            ),
          # Change the appearance of the text
          ___ = ___,
          ___ = "___",
          ___ = "___"
          )

ilo_dot_plot
コードを編集して実行