【问题标题】:ggplot/ggridges order of is mixed up when adding layers添加图层时,ggplot/ggridges 的顺序混淆了
【发布时间】:2022-01-04 22:40:40
【问题描述】:

我需要一些帮助。

我正在使用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))

【问题讨论】:

    标签: r ggplot2 ggridges


    【解决方案1】:

    根据您想要的结果,第二个选项是通过scale_y_discretelimits 参数设置顺序:

    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)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多