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.
Please note that each ind level (1:4) as four bar (1:4). All four bars for each ind are grouped in one place.
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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With