【问题标题】:How to make a stacked barplot with nested grouping variables?如何使用嵌套分组变量制作堆叠条形图?
【发布时间】:2019-02-13 10:02:02
【问题描述】:

我正在尝试制作一个包含两个变量的堆叠条形图。我想要的结果如下所示:

这是我数据的第一部分。还有 220 行:

      Type      Week Stage
   <chr>    <dbl> <dbl>
 1 Captured     1     2
 2 Captured     1     1
 3 Captured     1     1
 4 Captured     1     2
 5 Captured     1     1
 6 Captured     1     3
 7 Captured     1    NA
 8 Captured     1     3
 9 Captured     1     2
10 Captured     1     1

到目前为止,我还没有到任何地方,这是我的代码

library(data.table)
dat.m <- melt(newrstudio2, id.vars="Type")
dat.m

library(ggplot2)
ggplot(dat.m, aes(x=Type, y=value, fill=variable)) +
  geom_bar(stat="identity")

我想我需要计算每种类型每周每个阶段的观察次数?我已经尝试过长数据和宽数据,但我需要将星期与类型结合起来?我不知道,我很茫然。

【问题讨论】:

标签: r ggplot2 bar-chart


【解决方案1】:

另一种方式:

set.seed(123)
# sample data
my_data <- data.frame(Type = sample(c("W", "C"), 220, replace = TRUE),
                      Week = sample(paste0("Week ", 1:4), 220, replace = TRUE),
                      Stage = sample(paste0('S', 1:4), 220, replace = TRUE))
head(my_data)
library(ggplot2)

ggplot(my_data, aes(x = Type, fill = Stage)) + 
  geom_bar(aes(y = (..count..)/sum(..count..)), position = "fill") +
  facet_grid(. ~ Week, switch="both") + 
  scale_y_continuous(labels = scales::percent) + 
  ylab("Stage [%]") + 
  theme(strip.background = element_blank(),
        strip.placement = "outside",
        panel.spacing = unit(0, "lines"))

【讨论】:

  • 很高兴听到这个消息。你能接受我的回答吗?
【解决方案2】:

或者,我们可以使用基本图形。首先,您可能最感兴趣的是,我们应该重塑数据。

为此,我们可以每周拆分数据并对其运行dcast()

L <- lapply(split(d, d$week), function(x) 
  data.table::dcast(x, type ~ stage, value.var="stage", fun=length))

d2 <- do.call(rbind, L)  # transform back into a data frame

现在——with credits to @alemol——我们想要比例。

d2[-1] <- t(apply(d2[-1], 1, prop.table))

然后我们就可以相对简单地进行绘图了。请注意,barplot() 还为我们提供了一个条形坐标向量,我们稍后可以将其用于axis() 标签。

cols <- c("#ed1c24", "#ff7f27", "#00a2e8", "#fff200")  # define stage colors

par(mar=c(5, 5, 3, 5) + .1, xpd=TRUE)  # set plot margins

p <- barplot(t(d2[-1]), col=cols, border="white", space=rep(c(.2, 0), 5), 
        font.axis=2, xaxt="n", yaxt="n", xlab="Week")
axis(1, at=p, labels=rep(c("C", "W"), 5), tick=FALSE, line=0)
axis(1, at=apply(matrix(p, , 2, byrow=TRUE), 1, mean), labels=1:5, tick=FALSE, line=1)
axis(2, at=0:10/10, labels=paste0(seq(0, 100, 10), "%"), line=0, las=2)
legend(12, .5, legend=rev(names(d2[-1])), col=rev(cols), pch=15, title="Stage")

结果:


数据:

d <- structure(list(type = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 
1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 
2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 
2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 
2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L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, 
2L, 1L, 2L, 2L, 3L, 1L, 3L, 3L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 2L, 
1L, 2L, 1L, 3L, 3L, 2L, 1L, 3L, 4L, 2L, 1L, 2L, 4L, 3L, 4L, 2L, 
3L, 2L, 4L, 1L, 4L, 4L, 2L, 1L, 2L)), row.names = c(NA, -250L
), class = "data.frame")

【讨论】:

    【解决方案3】:

    这是你要找的吗:

    set.seed(123)
    # sample data
    my_data <- data.frame(Type = sample(paste0('T', 1:4), 220, replace = TRUE),
                          Week = sample(paste0('W', 1:4), 220, replace = TRUE),
                          Stage = sample(paste0('S', 1:4), 220, replace = TRUE))
    
    ggplot(my_data, aes(x=Week:Type, fill = Stage)) + geom_bar()
    

    【讨论】:

    • 这对我不起作用,收到警告消息“类型错误:周:NA/NaN 参数。数值表达式有 225 个元素:仅使用第一个元素。”省略了 NA,但仍然没有运气。另一个解决方案奏效了。
    猜你喜欢
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2021-11-16
    相关资源
    最近更新 更多