【问题标题】:Grouped ggplot boxplot in RR中的分组ggplot箱线图
【发布时间】:2019-05-23 11:18:08
【问题描述】:

对于示例数据框:

   df <- structure(list(year = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L), letter_group = c("A", "A", "A", "B", "B", "B", "C", 
"C", "C", "C", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C", 
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", 
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C"), value = c(2L, 
3L, 4L, 5L, 6L, 6L, 7L, 8L, 5L, 6L, 7L, 3L, 4L, 5L, 6L, 4L, 5L, 
6L, 2L, 3L, 4L, 4L, 5L, 6L, 7L, 8L, 5L, 3L, 2L, 4L, 5L, 6L, 4L, 
3L, 4L, 5L, 6L, 7L, 1L, 2L, 4L, 5L, 6L, 4L)), .Names = c("year", 
"letter_group", "value"), row.names = c(NA, -44L), class = c("tbl_df", 
"tbl", "data.frame"), spec = structure(list(cols = structure(list(
    year = structure(list(), class = c("collector_integer", "collector"
    )), letter_group = structure(list(), class = c("collector_character", 
    "collector")), value = structure(list(), class = c("collector_integer", 
    "collector"))), .Names = c("year", "letter_group", "value"
)), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

我正在尝试创建一个箱线图,其中包含 x 轴上的年份 - 以及按年份分组的“字母组”...

即第 1 年的 A、B、C,然后是小空间,然后是第 2 年的 A、B C,依此类推....

我有以下几点:

library(ggplot2)

p1 <- ggplot(df, aes(year, value))
p1 + geom_boxplot(aes(group=letter_group))

但这只会产生 3 个箱形图。

有人可以帮帮我吗?

【问题讨论】:

  • 您的分组变量似乎不是因素。 ggplot(df, aes(as.factor(year), value, fill=as.factor(letter_group))) + geom_boxplot() 你在找什么?
  • 感谢@nouse - 简单有效!完美的!在我的真实示例中,我有十个“字母组”——有没有办法指定变量的顺序? (我有 1-10,但 ggplot 正在订购它们 1、10、2-9)
  • @KT_1 你的意思是你的字母从 A 到 J 或者你的“字母”组是数字 1-10?试试factor(letter_group, levels = LETTERS[1:10])factor(letter_group, levels = 1:10)
  • 谢谢@Lyngbakr - 我的'letter_group'实际上被称为'deciles',运行1-10。您的第二个有用建议给出了错误: as.factor(deciles, levels = 1:10) 中的错误:未使用的参数 (levels = 1:10) 我做错了什么?
  • @KT_1 请注意,它是 factor 而不是 as.factor。 (有关差异的解释,请参阅 here。)

标签: r ggplot2


【解决方案1】:

@nouse 的解决方案(这是最好的解决方案)的替代方案是使用刻面。但是,分面的一个好处是您还可以在 x 轴上获得字母组标签。

定义数据结构

# Load library
library(ggplot2)

# Define data frame
df <- structure(list(year = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                              2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
                              3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                              4L, 4L), letter_group = c("A", "A", "A", "B", "B", "B", "C", 
                                                        "C", "C", "C", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C", 
                                                        "A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", 
                                                        "A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C"), 
                     value = c(2L, 3L, 4L, 5L, 6L, 6L, 7L, 8L, 5L, 6L, 7L, 3L, 4L, 5L, 6L, 4L, 5L, 
                               6L, 2L, 3L, 4L, 4L, 5L, 6L, 7L, 8L, 5L, 3L, 2L, 4L, 5L, 6L, 4L, 
                               3L, 4L, 5L, 6L, 7L, 1L, 2L, 4L, 5L, 6L, 4L)), 
                .Names = c("year", "letter_group", "value"), 
                row.names = c(NA, -44L), 
                class = c("tbl_df","tbl", "data.frame"), 
                spec = structure(list(cols = structure(list( ear = structure(list(), class = c("collector_integer", "collector")), 
                                                             letter_group = structure(list(), class = c("collector_character", "collector")), 
                                                             value = structure(list(), class = c("collector_integer",  "collector"))), 
                                                       .Names = c("year", "letter_group", "value")), 
                                      default = structure(list(), class = c("collector_guess", "collector"))), 
                                 .Names = c("cols", "default"), class = "col_spec"))

绘制结果

# Plot results
g <- ggplot(df)
g <- g + geom_boxplot(aes(letter_group, value))
g <- g + facet_grid(. ~ year, switch = "x")
g <- g + theme(strip.placement = "outside",
               strip.background = element_blank(),
               panel.background = element_rect(fill = "white"),
               panel.grid.major = element_line(colour = alpha("gray50", 0.25), linetype = "dashed"))
g <- g + ylab("Value") + xlab("Year & Letter Group")
print(g)

reprex package (v0.2.1) 于 2019 年 5 月 23 日创建

【讨论】:

    【解决方案2】:

    here,您的问题已基本得到解答。

    您的数据框不包含因子,因此您首先需要将分组变量转换为因子。然后,根据上面给出的链接,有两个选项。要么通过组合你的两个原始因子来构建一个新因子(如 z-cool 的答案所示) - 但这不会在 x 轴上的因子水平之间创建所需的空间 - 或者你需要将你的因子之一分配给 @ 987654322@,或col。就您而言,解决问题的最快方法是

    ggplot(df, aes(as.factor(year), value, fill=as.factor(letter_group))) + geom_boxplot()
    

    如果您不想为绘图着色,可以使用scale_fill_manualscale_color_manual 更改此设置,具体取决于您之前在aes 中的选择:

    ggplot(df, aes(as.factor(year), value, fill=as.factor(letter_group))) + geom_boxplot() +
      scale_fill_manual(values=c("white", "white", "white")) +
      theme(legend.position = "none")
    

    【讨论】:

      【解决方案3】:

      这应该可以工作

      library(tidyverse)
      df %>% 
        mutate(year_group = paste(year, letter_group)) %>% 
        ggplot(aes(year_group, value)) +
        geom_boxplot()
      

      【讨论】:

        猜你喜欢
        • 2017-01-06
        • 2023-04-03
        • 1970-01-01
        • 2016-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        相关资源
        最近更新 更多