Here is a user-defined function where you can manipulate between group graphs and within them and can be executed with any amount of data that you want to build.
multistack.bar <- function(x=list(x), betweenspace = 2, withinspace=0, ...){
Below is an example with three datasets. Note that all data has the same number of rows, but the column is different. Since the row values โโare stacked, there must be the same number, but you can make equal rows by setting NA or 0 for the missing group.
data1 <- matrix(c(1:5, rep(1,5), rep(4,5)), ncol=5) data2 <- matrix(c(2:6, rep(2,5), rep(3,5)), ncol=5) data3 <- matrix(c(2:6, rep(2,5), rep(3,5)), ncol=5) data4 <- matrix(c(1:4, rep(1,4), rep(4,4)), ncol=4) mylist <- list(data1, data2, data3, data4) multistack.bar(mylist, betweenspace=3, withinspace=0.1, col = c("pink", "blue", "purple"), xlab="groups", ylab="frequency", ylim = c(0, 16)) # you can decrease space between bars to 0 no space and between plots multistack.bar(mylist, betweenspace=1, withinspace=0, col = c("pink", "blue", "purple"), xlab="groups", ylab="frequency", ylim = c(0, 16)) # now you can use any thing you want just make it elegant or useful. legend(8, 14.8, c("A", "B", "C"), fill = c("pink", "blue", "purple"), ncol=3 ) abline(h=4, lty =2, col= "red")
