我需要一些帮助。
我使用的是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))

发布于 2022-01-05 00:32:43
根据您想要的结果,第二个选项是通过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)))

https://stackoverflow.com/questions/70585862
复制相似问题