【问题标题】:Define bins/subgroups (from continuous variable) to be plotted in box plot (ggplot in R)定义要在箱线图中绘制的箱/子组(来自连续变量)(R中的ggplot)
【发布时间】:2019-05-22 12:15:00
【问题描述】:

以鸢尾花数据集为例,我想绘制一个箱线图(仅适用于 setosa 物种),x 轴为Sepal.Length,y 轴为Petal.Length。但是,这首先需要将 x 轴的连续 Sepal.Length 数据分箱:Sepal.Length < 4.7, Sepal.Length 4.7 - 5, Sepal.Length 5 - 5.2 and Sepal.Length > 5.2. 其次,它需要将第一组和第三组组合在一起。我尝试了下面的代码,但这不起作用。任何建议,将不胜感激。谢谢。

library(ggplot2)
bin1 <- iris[iris$Sepal.Length < 4.7, ]
bin2 <- iris[iris$Sepal.Length >=4.7 & <5, ]
bin3 <- iris[iris$Sepal.Length >=5 & <5.2, ]
bin4 <- iris[iris$Sepal.Length >=5.2, ]
binA <- bin1 + bin3
order <- c(bin2, binA, bin4)
ggboxplot(iris[iris$Species == "setosa", ], x="Sepal.Length", y="Petal.Length") + scale_x_discrete(limits=order)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我会使用cut 函数来做你正在做的事情。之后,您可以使用fct_collapse 修改您的切割点。您可以执行以下操作:

    library(dplyr)
    library(forcats)
    library(ggplot2)
    
    iris %>% 
      filter(Species == "setosa") %>% 
      mutate(sub_species = cut(Sepal.Length, breaks = c(-Inf, 4.7, 5, 5.2, Inf))) %>% 
      mutate(sub_species = fct_collapse(sub_species,
                                        combined = c("(-Inf,4.7]", "(5.2, Inf]"))) %>% 
      ggplot(aes(sub_species, Petal.Length))+
      geom_boxplot()
    
    

    这会给你想要的。

    或者,您可以替换 cut 函数并使用 dplyr 的情况,当函数看起来像:

    iris %>% 
      filter(Species == "setosa") %>% 
      # Case when to cases
      mutate(sub_a = case_when( Sepal.Length < 4.7~"A",
                                Sepal.Length < 5~ "B",
                                Sepal.Length < 5.2~ "C",
                                TRUE~"D")) %>% 
      # Collapse A and D
      mutate(collapsed = ifelse(sub_a %in% c("A", "D"), "combined", sub_a)) %>% 
      ggplot(aes(collapsed, Petal.Length))+
      geom_boxplot()
    

    在 OP 评论中,问题被扩展为包括创建其他几个子类。为了解决这个问题,我将使用mutate 函数创建一些额外的子类别,然后使用gather 函数将它们全部拉到一个列中,同时保留每个子类中的数据(例如保持计数正确)。

    iris %>% 
      filter(Species == "setosa") %>% 
      # Case when to cases
      mutate(sub_a = case_when( Sepal.Length < 4.7~"A",
                                Sepal.Length < 5~ "B",
                                Sepal.Length < 5.2~ "C",
                                TRUE~"D")) %>% 
      # Collapse A and D
      mutate(collapsed1 = ifelse(sub_a %in% c("A", "C"), "A+C", sub_a)) %>% 
      mutate(collapsed2 = ifelse(sub_a %in% c("A", "C", "D"), "A+C+D", sub_a)) %>% 
      # Pull all the new categories together into a new column called subclass
      gather(new_cat, subclass, sub_a:collapsed2) %>% 
      # Filter to desired
      filter(subclass %in% c("B", "A+C", "D", "A+C+D")) %>% 
      ggplot(aes(subclass, Petal.Length))+
      geom_boxplot()
    
    

    【讨论】:

    • @SylviaRodriguez,没问题!很高兴为您提供帮助。
    • 再次感谢。我遇到了另一个我想实现的问题。除了绘制“B”、“C”和“组合”之外,我还想包括第四个由“A”、“C”和“D”组合组成的框。变异后,我无法弄清楚如何做到这一点。 (我更熟悉非管道脚本。)我会很感激你的建议。谢谢。
    • 为了完全清楚,我想绘制:“B”、“A+C”、“D”、“A+C+D”。谢谢。
    • @SylviaRodriguez 检查更新的答案。我认为这有你想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多