【问题标题】:How to color code a categorical variable in a mosaic如何对马赛克中的分类变量进行颜色编码
【发布时间】:2017-11-28 05:11:01
【问题描述】:

我正在尝试显示我的分类变量之间的关系。我终于将我的数据放入我认为是列联表中

subs_count
##                [,1] [,2] [,3] [,4]
## carbohydrate      2    0   11    2
## cellulose        18    0   60    0
## chitin            0    4    0    4
## hemicellulose    21    3   10    0
## monosaccharide    3    0    0    0
## pectin            8    0    2    2
## starch            1    0    4    0

每列代表一个有机体。所以对于我的情节,我投入了

barplot(subs_count, ylim = c(0, 100), col = predicted.substrate,
  xlab = "organism", ylab = "ESTs per substrate")

但我的基材颜色不一致。我做错了什么?

【问题讨论】:

    标签: r graph colors mosaic-plot


    【解决方案1】:

    您的数据似乎是一个matrix,其行名接近R 中的偶然事件table,但不完全相同。一些绘图方法对表格有额外的支持。

    更重要的是,我无法运行您的代码,因为不清楚 predicted.substrate 是什么。如果它是一个有 7 种颜色的调色板,那么它应该做你想做的事情(或者至少我认为你想做的事情)。

    我复制了你的数据:

    subs_count <- structure(c(2, 18, 0, 21, 3, 8, 1, 0, 0,
      4, 3, 0, 0, 0, 11, 60, 0, 10, 0, 2, 4, 2, 0, 4, 0, 0, 2, 0),
      .Dim = c(7L, 4L), .Dimnames = list(c("carbohydrate", "cellulose",
      "chitin", "hemicellulose", "monosaccharide", "pectin", "starch"), NULL))
    

    然后将它们转换为table by:

    subs_count <- as.table(subs_count)
    names(dimnames(subs_count)) <- c("EST", "Organism")
    

    然后我使用了来自colorspace 包的定性调色板:

    subs_pal <- colorspace::qualitative_hcl(7)
    

    而且你的条形图似乎是合理的:

    barplot(subs_count, ylim = c(0,100), col = subs_pal,
      xlab = "organism", ylab = "ESTs per substrate", legend = TRUE)
    

    马赛克显示(如您的标题所示)将是:

    mosaicplot(t(subs_count), col = subs_pal, off = 5, las = 1, main = "")
    

    为了可视化依赖模式(或者更确切地说是背离独立),使用独立模型的残差着色的马赛克图可能更有用。

    mosaicplot(t(subs_count), shade = TRUE, off = 5, las = 1, main = "")
    

    封装vcd(参见doi:10.18637/jss.v017.i03)中提供了更精致的阴影马赛克显示器版本。

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2019-06-18
      相关资源
      最近更新 更多