使用 R 将多个数据系列绘制成单个 bagplot。

5
让我们考虑R中aplpack库中包含的bagplot示例。 bagplot是箱线图的双变量推广,因此可以洞察数据点在两个轴上的分布。
示例的bagplot: car data bagplot 示例代码:
  # example of Rousseeuw et al., see R-package rpart
  cardata <- structure(as.integer( c(2560,2345,1845,2260,2440,
   2285, 2275, 2350, 2295, 1900, 2390, 2075, 2330, 3320, 2885,
   3310, 2695, 2170, 2710, 2775, 2840, 2485, 2670, 2640, 2655,
   3065, 2750, 2920, 2780, 2745, 3110, 2920, 2645, 2575, 2935,
   2920, 2985, 3265, 2880, 2975, 3450, 3145, 3190, 3610, 2885,
   3480, 3200, 2765, 3220, 3480, 3325, 3855, 3850, 3195, 3735,
   3665, 3735, 3415, 3185, 3690, 97, 114, 81, 91, 113, 97, 97,
   98, 109, 73, 97, 89, 109, 305, 153, 302, 133, 97, 125, 146,
   107, 109, 121, 151, 133, 181, 141, 132, 133, 122, 181, 146,
   151, 116, 135, 122, 141, 163, 151, 153, 202, 180, 182, 232,
   143, 180, 180, 151, 189, 180, 231, 305, 302, 151, 202, 182,
   181, 143, 146, 146)), .Dim = as.integer(c(60, 2)), 
   .Dimnames = list(NULL, c("Weight", "Disp.")))
  bagplot(cardata,factor=3,show.baghull=TRUE,
    show.loophull=TRUE,precision=1,dkmethod=2)
  title("car data Chambers/Hastie 1992")
  # points of y=x*x
  bagplot(x=1:30,y=(1:30)^2,verbose=FALSE,dkmethod=2)

aplpack的袋图似乎只支持绘制单个数据系列的“袋”。更有趣的是,在一个袋图中绘制两个(或三个)数据系列,通过视觉比较数据系列的“袋”,可以获得数据分布差异的见解。是否有人知道如何在R中实现这一点(如果可以的话)?


bagplot有一个add参数。这是通过仔细阅读帮助页面可以回答的问题。 - IRTFM
1
还可以考虑通过 par('mfrow')par('mfcol')layout()par('fig') 进行分面。 - r2evans
1个回答

10

如果我们修改一些aplpack :: bagplot代码,我们就可以为ggplot2创建一个新的geom。然后,我们可以按照通常的ggplot2方式比较数据集内的组别。这里是一个例子:

library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width, 
                 colour = Species, fill = Species)) +
       geom_bag() +
       theme_minimal()

输入图像描述

我们可以使用袋形图来展示这些点:

ggplot(iris, aes(Sepal.Length, Sepal.Width, 
                     colour = Species, fill = Species)) +
           geom_bag() +
           geom_point() + 
           theme_minimal()

输入图像描述

这是 geom_bag 和修改后的 aplpack::bagplot 函数的代码:https://gist.github.com/benmarwick/00772ccea2dd0b0f1745


最佳安装R的方法是什么? - reas0n
2
@ElijahRockers 我在代码片段中添加了一些代码,以展示如何加载函数,以便您可以在本地使用它们:https://gist.github.com/benmarwick/00772ccea2dd0b0f1745#file-002_bag_demo-r-L1 - Ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接