0

I would like to generate a bar chart similar to the one shown below. I would like to use rhoas x-axis and power as y-axis. And I have 2 categorical variables m and c. How can I generate a grouped bar with 2 grouping variables?

Any other idea of visualizing will be welcomed.

In this picture they use m as color, rho and c as x axis variables.

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")
Hilda
  • 15
  • 4
  • Hi @XLZ and welcome to StackOverflow! Could you post a `dput` of your sample data so we can more easily work with this? Generally speaking, I'd recommend working with `ggplot` and doing some custom grouping. – Kris Dec 04 '19 at 17:45
  • As mentioned by @Kris, please consider providing a [reproducible example] of your dataset(https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (see link enclosed). I provided a putative solution but without example, it's hard to be sure it will work – dc37 Dec 04 '19 at 18:11
  • @Kris Thank you very much! I edited the question. Did I make it clear now? – Hilda Dec 04 '19 at 21:16
  • @dc37 Thank you very much! I edited the question. Did I make it clear now? – Hilda Dec 04 '19 at 21:17
  • @XLZ, I edited my answer accordingly to your question. Let me know if it is what you are looking for – dc37 Dec 04 '19 at 23:35

1 Answers1

0

Personally, I will use facetting to get a grouped bar chart with two categorical variables.

With your example, I will try something like that:

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))

Now, you can plot it by defining m for color and c will be the faceted variables

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

And you get something like that enter image description here

Does it look what you are expecting ?

dc37
  • 15,840
  • 4
  • 15
  • 32