【问题标题】:R barplot TRUE/FALSE condition tableR barplot TRUE/FALSE 条件表
【发布时间】:2012-04-06 20:03:59
【问题描述】:

我有一个关于 barplot 函数的简单问题。我有一个像这样的 TRUE/FALSE 表:

打印(一)

假真

35517 1160

现在我想创建一个带有一列的条形图(因此该列的长度为 36677)。在第 2 列中,颜色必须包含 TRUE/FALSE 条件。就这么简单吗?

我想像这样:barplot(a, beside=FALSE) 但是我创建了 2 列并排。

谢谢!

【问题讨论】:

    标签: r conditional


    【解决方案1】:

    然后将a指定为矩阵:

    a <- c("FALSE" = 35517,"TRUE" = 1160)
    a <- as.matrix(a)
    barplot(a)
    

    【讨论】:

      【解决方案2】:

      一个选项是使用mosaicplot()

      aa <- c(rep(FALSE, 35517), rep(TRUE, 1160))
      a <- table(aa)
      
      mosaicplot(a, dir = "h", off = 0, color = TRUE)
      

      查看?mosaicplot了解更多信息

      【讨论】:

        【解决方案3】:

        使用 ggplot

        #install.packages("ggplot2")
        
        library(ggplot2)
        
        df<-data.frame(value=c("TRUE", "FALSE"), counts=c(1160,35517), index=1)
        
        qplot(data=df, x=factor(index), y=counts, geom="bar", fill=value)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 2015-07-28
          • 2020-08-24
          • 2023-04-04
          • 2018-07-17
          • 2022-07-07
          • 1970-01-01
          相关资源
          最近更新 更多