Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to produce nice qualitative bar composition graphs

Here is my data:

ind <- c( rep(1, 20), rep (2, 20), rep (3, 20), rep(4, 20))
bar <- c(rep(rep(1:4, each = 5),  4))

mark <- c(rep ("A", 5), rep("B", 5), rep("C",5), rep("D",5),
          rep("B", 20), rep("C", 20), "A", "B", "C", "A", "C","A",
          "D", "D", "D", "D","A", "C", "C", "A", "B", "B","B",
          "C","C","C")

dataf <- data.frame(ind, bar, mark)

Each individual (ind) has 4 bars in order and are color fomatted. There should be larger space between individuals. I might have any number of individuals. The following expected graph drawn in excel, I want to create more pretty graph in R instead.

enter image description here

Please note that each ind level (1:4) as four bar (1:4). All four bars for each ind are grouped in one place.

like image 529
jon Avatar asked Jan 18 '23 00:01

jon


1 Answers

You can try using the rect function to individually plot each rectangle. Try running a loop to iterate through each box in the stack:

par(mfrow=c(2, 2))

for(i in unique(ind)) {
  plot(0, col=0, bty="n", xaxt="n", yaxt="n", xlab = i, ylab = "", xlim=c(0, 5), ylim=c(-6, -1))
  for(j in 1:4) {
    for(k in 1:5) {
      rect(j-0.4, -k-1, j+0.4, -k, col = which(unique(mark)==matrix(mark[ind==i], 5, 4)[k,j])+1, border = rgb(0,0,0,0))
      text(j, -k-0.5, labels = matrix(mark[ind==i], 5, 4)[k,j])
    }
    rect(j-0.4, -6, j+0.4, -1)
  }
}

enter image description here

like image 150
oeo4b Avatar answered Jan 22 '23 19:01

oeo4b