【问题标题】:ggplot2: Boxplots with points and fill separation [duplicate]ggplot2:带有点和填充分离的箱线图[重复]
【发布时间】:2017-06-05 12:16:20
【问题描述】:

我有一个可以通过两个分隔符分割的数据。一是年份,二是字段特征。

box<-as.data.frame(1:36)

box$year <- c(1996,1996,1996,1996,1996,1996,1996,1996,1996,
              1997,1997,1997,1997,1997,1997,1997,1997,1997,
              1996,1996,1996,1996,1996,1996,1996,1996,1996,
              1997,1997,1997,1997,1997,1997,1997,1997,1997)
box$year <- as.character(box$year)

box$case <- c(6.40,6.75,6.11,6.33,5.50,5.40,5.83,4.57,5.80,
              6.00,6.11,6.40,7.00,NA,5.44,6.00,  NA,6.00,
              6.00,6.20,6.40,6.64,6.33,6.60,7.14,6.89,7.10,
              6.73,6.27,6.64,6.41,6.42,6.17,6.05,5.89,5.82)

box$code <- c("L","L","L","L","L","L","L","L","L","L","L","L",
              "L","L","L","L","L","L","M","M","M","M","M","M",
              "M","M","M","M","M","M","M","M","M","M","M","M")

colour <- factor(box$code, labels = c("#F8766D", "#00BFC4"))

在箱线图中,我想在它们上面显示点,以查看数据是如何分布的。每年只需一个箱线图即可轻松完成:

ggplot(box, aes(x = year, y = case, fill = "#F8766D")) +
  geom_boxplot(alpha = 0.80) +
  geom_point(colour = colour, size = 5) +
  theme(text = element_text(size = 18),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        legend.position = "none")

但随着我在其中添加填充参数,它变得更加复杂:

ggplot(box, aes(x = year, y = case, fill = code)) +
  geom_boxplot(alpha = 0.80) +
  geom_point(colour = colour, size = 5) +
  theme(text = element_text(size = 18),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        legend.position = "none")

现在的问题是:如何将这些点移动到它们所属的箱线图轴上?蓝色指向蓝色箱线图,红色指向红色箱线图。

【问题讨论】:

标签: r ggplot2 boxplot


【解决方案1】:

我看到你已经接受了@JakeKaupp 的好答案,但我想我会提出一个不同的选择,使用geom_dotplot。您正在可视化的数据相当小,那么为什么不放弃箱线图呢?

ggplot(box, aes(x = factor(year), y = case, fill = code))+
    geom_dotplot(binaxis = 'y', stackdir = 'center',
                 position = position_dodge())

【讨论】:

  • 谢谢!但是我的数据集很大,所以我必须使用箱线图。这个问题的集合只是其中的一小部分。
【解决方案2】:

就像 Henrik 所说,使用 position_jitterdodge()shape = 21。你也可以稍微清理一下你的代码:

  1. 无需定义框,然后逐个填充
  2. 如果您愿意,可以让ggplot 散列颜色并跳过构建颜色因子。如果要更改默认值,请查看 scale_fill_manualscale_color_manual

    box <- data.frame(year = c(1996,1996,1996,1996,1996,1996,1996,1996,1996,
                      1997,1997,1997,1997,1997,1997,1997,1997,1997,
                      1996,1996,1996,1996,1996,1996,1996,1996,1996,
                      1997,1997,1997,1997,1997,1997,1997,1997,1997),
                      case  = c(6.40,6.75,6.11,6.33,5.50,5.40,5.83,4.57,5.80,
                      6.00,6.11,6.40,7.00,NA,5.44,6.00,  NA,6.00,
                      6.00,6.20,6.40,6.64,6.33,6.60,7.14,6.89,7.10,
                      6.73,6.27,6.64,6.41,6.42,6.17,6.05,5.89,5.82),
                      code = c("L","L","L","L","L","L","L","L","L","L","L","L",
                      "L","L","L","L","L","L","M","M","M","M","M","M",
                      "M","M","M","M","M","M","M","M","M","M","M","M"))
    
    ggplot(box, aes(x = factor(year), y = case, fill = code)) +
      geom_boxplot(alpha = 0.80) +
      geom_point(aes(fill = code), size = 5, shape = 21, position = position_jitterdodge()) +
      theme(text = element_text(size = 18),
            axis.title.x = element_blank(),
            axis.title.y = element_blank(),
            panel.grid.minor.x = element_blank(),
            panel.grid.major.x = element_blank(),
            legend.position = "none")
    

【讨论】:

  • 1997年最低圆圈左边的黑点是什么?
  • 来自geom_boxlot的点/潜在异常值
  • 如果添加一个组来改变点的形状,它似乎不再起作用了......
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
相关资源
最近更新 更多