0

助けが必要です。

私は使用してggridgesいますが、以下に例示する問題が発生します。

y 軸の順番、つまり (1, 10, 11, 2) が間違っています。最後の図の y 軸の順序を (1, 2, 3, 10, 20) にしたい 以下のコード:

library(ggplot2)
library(ggridges)

iris1 <- iris
iris2 <- iris
# change levels 
levels(iris1$Species) <- c(1,2,10)
levels(iris2$Species) <- c(1, 3, 20)
# offset iris2 so we can see it
iris2$Sepal.Length <- iris2$Sepal.Length + 1

# PLots with correct order of y
ggplot() + geom_density_ridges(data = iris1, aes(x = Sepal.Length, y = Species))

ここに画像の説明を入力

# the addition of layers throws off the order
ggplot() + 
  geom_density_ridges(data = iris1, aes(x = Sepal.Length, y = Species)) + 
  geom_density_ridges(data = iris2, aes(x = Sepal.Length, y = Species))

ここに画像の説明を入力

4

1 に答える 1

1

目的の結果に応じて、2 番目のオプションは、次のlimits引数を介して順序を設定することですscale_y_discrete

library(ggplot2)
library(ggridges)

ggplot() + 
  geom_density_ridges(data = iris1, aes(x = Sepal.Length, y = Species)) + 
  geom_density_ridges(data = iris2, aes(x = Sepal.Length, y = Species)) +
  scale_y_discrete(limits = as.character(c(1, 2, 3, 10, 20)))

于 2022-01-05T00:32:43.700 に答える