R言語による医療データ分析

R言語によるデータ分析のオンラインコースを中心に、さまざまなデータ分析について記載してあります。

065 補足:X軸のラベルの回転

まとめ一覧

補足:themeでX軸のラベルを回転させる

  • themeでの個別設定が必要になる場合について解説します
table <- data.frame(
  item_name = c("究極のマスクメロンアイスクリーム",
                "イチゴたっぷりショートケーキイタリア風",
                "和栗の贅沢ブラックモンブラン",
                "朝どれ卵のなめらかプリン",
                "マンゴーと南国フルーツのタルト",
                "フルーツをたっぷりつかったロールケーキ"),
  uriage_kosu = c(39,42,73,88,93,132)
)

ggplot(table) + 
  geom_bar(aes(item_name, uriage_kosu), stat="identity")
  • このようにX軸が重なってしまったというときは、themeで設定を変えましょう
ggplot(table) + 
  geom_bar(aes(item_name, uriage_kosu), stat="identity") +
  theme(axis.text.x = element_text(angle = 45, hjust=1))
  • ただし、theme_classicなどを設定すると上書きされるので、
ggplot(table) + 
  geom_bar(aes(item_name, uriage_kosu), stat="identity") + 
  theme(axis.text.x = element_text(angle=45, hjust = 1)) +
  theme_classic()
  • 順番を入れ替えましょう
ggplot(table) + 
  geom_bar(aes(item_name, uriage_kosu), stat="identity") +
  theme_classic() +
  theme(axis.text.x = element_text(angle=45, hjust = 1))

まとめ一覧