ggplotでy軸名を折り返したいとき

  • テストデータ
df <- tibble(
  label_grp = c("short", "Medium label",
                "a very long axis label that needs to be folded"),
  some_value = c(13633,20824,42000)
)
  • まずは普通にggplot
  • このままだと折り返してくれない
ggplot(data = df) +
  geom_col(aes(y = label_grp, 
               x = some_value,
               fill = label_grp))
  • str_wrapscale_y_discreteかますと折り返してくれる
ggplot(data = df) +
  geom_col(aes(y = label_grp, 
               x = some_value,
               fill = label_grp)) +
  scale_y_discrete(labels = function(x) str_wrap(x, width = 15))