【问题标题】:Boxplots for groups?组的箱线图?
【发布时间】:2013-01-14 12:42:40
【问题描述】:

我有一个数据集(测试),如下所示:

Type    Met1    Met2    Met3    Met4
TypeA   65  43  97  77
TypeA   46  25  76  77
TypeA   44  23  55  46
TypeA   46  44  55  77
TypeA   33  22  55  54
TypeB   66  8   66  47
TypeB   55  76  66  65
TypeB   55  77  88  46
TypeB   36  67  55  44
TypeB   67  55  76  65

我已经检查了很多关于箱线图的链接,但我仍然没有成功获得我想要的箱线图类型。我希望为所有大都会(Met1、Met2、Met3、Met4)制作一个箱线图,其中我的 X 轴具有 A 型(黄色、橙色)。本质上,我想要类似下面的东西(取自here):

我正在尝试类似的东西,

boxplot(formula = len ~ Type , data = test, subset == "TypeA")
boxplot(formula = len ~ Type , data = test, subset == "TypeA", add=TRUE)
Legend(legend = c( "typeA", "typeB" ), fill = c( "yellow", "orange" ) )

但我无法解决任何问题。谁能帮助我知道如何以正确的方式在我的测试数据上制作这样的箱线图?

【问题讨论】:

  • 您需要学会仔细阅读代码,例如,subset == "TypeA" 显然不是您链接到的示例中显示的内容。

标签: r boxplot


【解决方案1】:

您可以使用reshape 函数创建一个长列,其中包含来自 Met1、..、Met4 列的所有数据。它还创建一个列 (temp$time) 来识别数据来自哪个列,您可以使用它来对箱形图进行分层 (temp$Type*temp$time)。

df <- read.table(text=
"Type Met1 Met2 Met3 Met4
TypeA   65  43  97  77
TypeA   46  25  76  77
TypeA   44  23  55  46
TypeA   46  44  55  77
TypeA   33  22  55  54
TypeB   66  8   66  47
TypeB   55  76  66  65
TypeB   55  77  88  46
TypeB   36  67  55  44
TypeB   67  55  76  65",header=TRUE)

temp <- reshape(df, direction='long', varying = 2:5, sep='')

boxplot(temp$Met ~ temp$Type*temp$time, col=c("yellow", "orange"))

【讨论】:

    【解决方案2】:

    像这样,

    test <- structure(list(Type = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
        2L, 2L, 2L), .Label = c("TypeA", "TypeB"), class = "factor"), 
        Met1 = c(65L, 46L, 44L, 46L, 33L, 66L, 55L, 55L, 36L, 67L
        ), Met2 = c(43L, 25L, 23L, 44L, 22L, 8L, 76L, 77L, 67L, 55L
        ), Met3 = c(97L, 76L, 55L, 55L, 55L, 66L, 66L, 88L, 55L, 
        76L), Met4 = c(77L, 77L, 46L, 77L, 54L, 47L, 65L, 46L, 44L, 
        65L)), .Names = c("Type", "Met1", "Met2", "Met3", "Met4"), 
        class = "data.frame", row.names = c(NA, -10L))
    
    
    # install.packages("ggplot2", dependencies = TRUE)
    require(ggplot2)
    require(reshape2)
    df <- melt(test)
    p <- ggplot(df, aes(factor(variable), value)) + geom_boxplot(aes(fill = Type))
    p
    

    你看看the geom_boxplot manual page

    【讨论】:

      【解决方案3】:

      正如其他人所说,首先你需要融合你的数据。

      df <- read.table(text="Type    Met1    Met2    Met3    Met4
      TypeA   65  43  97  77
      TypeA   46  25  76  77
      TypeA   44  23  55  46
      TypeA   46  44  55  77
      TypeA   33  22  55  54
      TypeB   66  8   66  47
      TypeB   55  76  66  65
      TypeB   55  77  88  46
      TypeB   36  67  55  44
      TypeB   67  55  76  65",header=TRUE)
      
      library(reshape2)
      df2 <- melt(df)
      
      boxplot(
        formula = value ~ variable,
        data    = df2,
        boxwex  = 0.25,
        at      = 1:4 - 0.2,
        subset  = Type == "TypeA",
        col     = "yellow",
        main    = "blah",
        xlab    = "x",
        ylab    = "y",
        ylim    = c( 0, ceiling( max( df2$value ) ) + 1 ),
        yaxs    = "i" )
      
      
      boxplot(
        formula = value ~ variable,
        data    = df2,
        boxwex  = 0.25,
        at      = 1:4 + 0.2,
        subset  = Type == "TypeB",
        col     = "orange",
        add     = TRUE )
      

      【讨论】:

        【解决方案4】:

        ggplot2 的解决方案。

        首先,使用melt 将您的数据框test 转换为长格式:

        library(reshape2)
        test.m <- melt(test)
        

        绘制数据:

        library(ggplot2)
        ggplot(test.m, aes(x = variable, y = value, fill = Type)) +
          geom_boxplot() +
          scale_fill_manual(values = c("yellow", "orange"))
        

        【讨论】:

          猜你喜欢
          • 2020-05-22
          • 2021-09-08
          • 2016-09-14
          • 2013-05-11
          • 2021-02-03
          • 2021-03-08
          • 1970-01-01
          • 2019-04-29
          • 2011-08-27
          相关资源
          最近更新 更多