【问题标题】:Combine groups into one group to display in boxplot (ggplot2, R)将组合并为一组以在箱线图中显示(ggplot2,R)
【发布时间】:2019-10-18 15:01:41
【问题描述】:

我以 mtcars 数据集为例,我使用了这段代码。

library(ggplot2)
library(ggsci)
ggviolin(mtcars, x="cyl", y="disp", fill="cyl", palette="jco", facet.by = "am")

对于每个方面,我想在 x 轴上添加第四个类别(可能称之为“6or8”),其中组合了 6 和 8 缸组(但不是 4 缸组) .我找到了this similar post,但它对我没有帮助,因为我的方面和添加了两个而不是所有类别。

有人有什么建议吗?谢谢。

【问题讨论】:

    标签: r ggplot2 boxplot violin-plot


    【解决方案1】:

    你可以试试这个:

    > newmtcars <- rbind(mtcars %>% mutate(cyl = as.character(cyl)),
    +                    mtcars %>% filter(cyl %in% c(6,8)) %>% mutate(cyl = '6or8')) %>% arrange(cyl)
    > ggviolin(newmtcars, x="cyl", y="disp", fill="cyl", palette="jco", facet.by = "am")
    

    您可以手动更改cyl 的级别以更改图中的顺序(例如,如果您希望“6or8”成为第一个/最后一个级别)。

    【讨论】:

    • 太完美了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 2011-05-31
    • 2015-09-11
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多