facet_wrap()
引数がないことで認識されていspace = "free"
ます ( https://github.com/tidyverse/ggplot2/issues/2933 )。これにより、プロットの y 軸で間隔の問題が発生する可能性があります。
次のコードを使用して上の図を作成します。
library(tidyverse)
p <-
mtcars %>%
rownames_to_column() %>%
ggplot(aes(x = disp, y = rowname)) + geom_point() +
facet_wrap(~ carb, ncol = 1, scales = "free_y")
facet_grid
一方、space = "free"
引数があります。適切な y 軸の間隔を確保します。
次のコードを使用して上の図を作成します。
p <-
mtcars %>%
rownames_to_column() %>%
ggplot(aes(x = disp, y = rowname)) + geom_point() +
facet_grid(carb ~ ., scales = "free_y", space = "free_y")
これに関する問題は、ラベルが上部ではなく側面にあることです。ファセット ラベルが長くなり、ファセット内の行が少なくなることがあります。これは、ファセット ラベルが切り取られることを意味します。
ggforceパッケージからの解決策があります ( https://github.com/tidyverse/ggplot2/issues/2933の ilarischeinin によるコメント)。
p <-
mtcars %>%
rownames_to_column() %>%
ggplot(aes(x = disp, y = rowname)) + geom_point()
p + ggforce::facet_col(vars(carb), scales = "free_y", space = "free")
ただし、 ggplot2を離れるには制限があります。たとえば、最終的には 2 列の図が必要ですが、この機能はggforce では実現できないようです。引数facet_wrap()
を利用できるように、を使用して同じ結果を生成する方法はありますか?ncol()