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

根据您想要的结果,第二个选项是通过以下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 回答