如何生成带有2个类别变量的分组条形图

时间:2019-12-04 17:36:26

标签: r ggplot2

我想生成类似于以下所示的条形图。我想将rho用作x轴,将power用作y轴。我有2个类别变量mc。如何生成带有2个分组变量的分组条形图?

任何其他可视化的想法都将受到欢迎。

在这张照片中,他们使用m作为颜色,使用rho和c作为x轴变量。

different color: m

> dput(ds2)
structure(list(X = c(105L, 135L, 141L, 165L, 171L, 177L, 201L, 
207L, 213L, 231L, 237L, 243L, 249L, 267L, 273L, 279L, 285L, 303L, 
309L, 315L, 321L, 465L, 495L, 501L, 525L, 531L, 537L, 561L, 567L, 
573L, 591L, 597L, 603L, 609L, 627L, 633L, 639L, 645L, 861L, 891L, 
897L, 921L, 927L, 933L, 957L, 963L, 969L, 1287L, 1293L), d_s = c(0.2, 
0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 
0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 
0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 
0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2), m = structure(c(6L, 
5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L, 
3L, 4L, 5L, 6L, 6L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 3L, 4L, 5L, 
6L, 3L, 4L, 5L, 6L, 6L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 5L, 6L
), .Label = c("5", "10", "15", "20", "25", "30"), class = "factor"), 
    c = structure(c(3L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 
    7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 4L, 5L, 5L, 6L, 6L, 
    6L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 6L, 7L, 7L, 
    8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L), .Label = c("4", "6", "8", 
    "10", "12", "14", "16", "18", "20"), class = "factor"), rho = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
    0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.03, 0.03, 
    0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.05, 0.05), n0 = c(1984L, 
    1491L, 748L, 1984L, 748L, 529L, 910L, 552L, 437L, 1984L, 
    647L, 461L, 387L, 1035L, 529L, 409L, 355L, 748L, 461L, 375L, 
    333L, 3055L, 2018L, 952L, 2647L, 916L, 638L, 1094L, 650L, 
    512L, 2464L, 751L, 530L, 443L, 1202L, 601L, 462L, 401L, 12390L, 
    4206L, 1528L, 5721L, 1370L, 909L, 1602L, 890L, 686L, 25498L, 
    2685L), results = c(TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, 
    TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, 
    TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, 
    FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, 
    TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, 
    FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE), power = c(0.85, 
    0.87, 0.92, 0.89, 0.89, 0.89, 0.85, 0.89, 0.87, 0.89, 0.94, 
    0.9, 0.92, 0.88, 0.93, 0.88, 0.92, 0.88, 0.9, 0.86, 0.85, 
    0.88, 0.9, 0.95, 0.94, 0.9, 0.87, 0.84, 0.89, 0.93, 0.91, 
    0.86, 0.87, 0.87, 0.91, 0.88, 0.91, 0.89, 0.95, 0.9, 0.89, 
    0.95, 0.95, 0.87, 0.85, 0.9, 0.91, 0.91, 0.87)), row.names = c(37L, 
51L, 55L, 67L, 71L, 75L, 87L, 91L, 95L, 104L, 108L, 112L, 116L, 
125L, 129L, 133L, 137L, 147L, 151L, 155L, 159L, 211L, 226L, 230L, 
242L, 246L, 250L, 262L, 266L, 270L, 279L, 283L, 287L, 291L, 301L, 
305L, 309L, 313L, 390L, 405L, 409L, 421L, 425L, 429L, 441L, 445L, 
449L, 563L, 567L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

就个人而言,我将使用构面获取具有两个分类变量的分组条形图。

以您的示例为例,我将尝试以下操作:

df <- ds2[,c("rho","m","c","power")]
df$rho <- as.factor(df$rho)
df$m <- as.factor(df$m)
df$c <- as.factor(df$c)
df <- rbind(df, cbind(expand.grid(rho = levels(df$rho), m = levels(df$m), c= levels(df$c)), power = NA))

现在,您可以通过为颜色定义m来绘制它,而c将是多面变量

library(ggplot2)
ggplot(df, aes(x = rho, y = power, colour = m))+
  geom_bar(aes(fill = m), stat = "identity", position = "dodge") +
  facet_grid(.~c)

你得到类似的东西 enter image description here

看起来像您期望的那样吗?