当前位置:  开发笔记 > 编程语言 > 正文

ggplot2:带有点和填充分离的箱形图

如何解决《ggplot2:带有点和填充分离的箱形图》经验,为你挑选了2个好方法。

我有一个数据可以通过两个分隔符分开.一个是年和二是场特征.

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

在此输入图像描述

但是当我在其中添加fill参数时,它变得更加复杂:

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

在此输入图像描述

现在的问题是:如何将这些点移动到boxplot轴上,它们属于哪个?蓝色指向蓝色箱图,红色指向红色.



1> Jake Kaupp..:

就像Henrik说的那样,使用position_jitterdodge()shape = 21.您也可以稍微清理一下代码:

    无需定义框,然后逐个填充

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

在此输入图像描述



2> bouncyball..:

我看到您已经接受@JakeKaupp的不错的回答,但是我认为我会使用抛出其他选择geom_dotplot。您要可视化的数据很小,那么为什么不放弃箱线图呢?

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

推荐阅读
mobiledu2402851377
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有