ggplot2嵌套在水平条中的垂直条[产品图]

时间:2015-08-27 18:22:23

标签: r plot ggplot2

我有一组数据,其中一个因子嵌套在另一个因子中。

    Family  Member Count
1  family1 Brother     3
2  family2 Brother     1
3  family3 Brother     1
4  family1     Dad     1
5  family2     Dad     1
6  family3     Dad     1
7  family1     Mom     1
8  family2     Mom     1
9  family3     Mom     0
10 family1     Pet     2
11 family2     Pet     0
12 family3     Pet     3
13 family1  Sister     3
14 family2  Sister     5
15 family3  Sister     3

我想显示父级别的水平条形图,然后显示子级别的垂直条形图。我偶然发现了哈德利的一篇论文,概述了如何做到这一点http://vita.had.co.nz/papers/prodplots.pdf。他在GitHub回购https://github.com/hadley/productplots中做过的初步工作,但由于缺乏兴趣,开发工作已停止。我可以用这个回购几乎产生我以后但不安静的东西。我想知道是否有人可以帮忙完成剩下的工作。我知道还有其他的绘图方法,但是这个方法特别好,因为它使用了预先注意的属性,即在层次结构的两个层次上将长度与公共零点进行比较。

这是我从Hadely的回购中得到的:

enter image description here

我想:

  1. 删除额外的父级标签
  2. 用浅色填充父条
  3. 将x轴刻度更改为原始计数
  4. enter image description here

    这里的代码产生了原始情节:

    devtools::install_github("hadley/productplots")
    library(productplots)
    library(ggplot2)
    
    dat <- structure(list(Family = c("family1", "family2", "family3", "family1", 
        "family2", "family3", "family1", "family2", "family3", "family1", 
        "family2", "family3", "family1", "family2", "family3"), Member = structure(c(1L, 
        1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("Brother", 
        "Dad", "Mom", "Pet", "Sister"), class = "factor"), Count = c(3L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 2L, 0L, 3L, 3L, 5L, 3L)), row.names = c(NA, 
        -15L), .Names = c("Family", "Member", "Count"), class = "data.frame")
    
    dat2 <- dat[rep(1:nrow(dat), dat[["Count"]]), ]
    
    
    fill <- list(
      aes(fill = Member),
      theme(legend.position = "none"),
      scale_fill_manual(values = c(
        "Dad" = "#2B2D42", 
        "Mom" = "#7A7D7F", 
        "Sister" = "#B1BBCF", 
        "Brother" = "#6E0B21", 
        "Pet" = "#9B4D73", 
        "NA" = "grey50")))
    
    m <- prodplot(dat2, ~ Member + Family, c("hbar", "vbar"), na.rm = TRUE,
             levels = NA) + fill + theme_bw()
    m + scale_x_continuous(expand = c(0,0), limits = c(0, 1.1)) +
        theme(panel.grid=element_blank())
    

    如果使用的话,我还复制了两个单独的图:

    library(ggplot2)
    
    ggplot(dat, aes(Family, weight=Count)) +
        geom_bar(fill="grey80") +
        coord_flip() + 
        scale_y_continuous(expand = c(0,0), limits = c(0, 11)) +
        theme_bw()
    
    ggplot(dat, aes(Family, weight=Count)) +
        geom_bar(aes(fill = Member), position = "dodge") +
        scale_y_continuous(expand = c(0,0), limits = c(0, 6)) +
        theme_bw() +
        facet_wrap(~Family, ncol=1, scales="free_x") +
        theme(text=element_blank(), panel.grid=element_blank())
    

    其他图形系统解决方案也欢迎。

    编辑试图像这样填充但是所有的条纹都变成灰色而不仅仅是父条:

    lvls <- unique(dat[["Family"]])
    
    fill2 <- list(
      aes(fill = Family),
      theme(legend.position = "none"),
      scale_fill_manual(values = setNames(rep("grey90", length(lvls)), lvls)))
    
    m <- prodplot(dat2, ~ Member + Family, c("hbar", "vbar"), na.rm = TRUE,
             levels = NA) + fill + fill2 + theme_bw()
    m + scale_x_continuous(expand = c(0,0), limits = c(0, 1.1)) +
        theme(panel.grid=element_blank())
    

1 个答案:

答案 0 :(得分:3)

如何&#39; S:

model = new createjs.Bitmap(new lib["c"+ 1]()); 
a {_listeners: null, _captureListeners: null, alpha: 1, cacheCanvas: null, cacheID: 0…}
console.log(model.image)
VM281:2 lib.c1 {spriteSheet: a, paused: true, currentAnimationFrame: 0, _animation: null, currentAnimation: null…}
undefined
console.log(model.image.width)
VM282:2 undefined
undefined

enter image description here