Ecco la funzione personalizzata in cui è possibile manipolare tra e all'interno di grafici di gruppo e può essere fatto con qualsiasi numero di dati che si desidera tracciare.
multistack.bar <- function(x=list(x), betweenspace = 2, withinspace=0, ...){
# note that number of rows in each component matrix of the list
# should be equal
# if you have missing value use "NA", to make it complete
# number of column can differ
mylist <- x
space = list()
space[[1]] <- c(rep(withinspace,ncol(mylist[[1]])),betweenspace)
for (i in 2:length(mylist)){
if(i == length(mylist)){
space[[i]] <- c(rep(withinspace,ncol(mylist[[i]])-1))
} else{
space[[i]] <- c(rep(withinspace,ncol(mylist[[i]])-1),betweenspace)
}
}
un.space <- c(unlist(space))
newdata <- do.call("cbind", mylist)
barplot(newdata, space= un.space, ...)
}
Di seguito è riportato un esempio con tre set di dati. Nota che tutti i dati hanno lo stesso numero di righe ma la colonna è diversa. Dato che i valori delle righe sono impilati, dovrebbe essere lo stesso numero, ma è possibile rendere uguali le righe inserendo NA o 0 per il gruppo mancante.
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")

Forse qualcosa di simile: 'barplot (cbind (data1, data2), spazio = c (rep (0, ncol (data1)), 2, rep (0, ncol (data2)))) ' – WaltS
@WaltS, dovresti postare come risposta. (ma cambia in 'ncol (data2) -1' per rimuovere l'avviso) – user20650