我想创建一个组合条形图,其中data1和data2在堆叠中,而data1和data2在基础R图形中分组。
安排中有这样的事情:
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)
# stacked bar
barplot(data1)
#grouped var
barplot(data1, beside=TRUE)
知道我该怎么办?我知道我们可以使用不同的图或刻面来做到这一点,但我想在一个图中并排显示它们。
答案 0 :(得分:4)
您可以尝试使用/**
* A custom function that we created to dynamically update the content in the
* header.
* @param {Array} cellsNotValidated Array of column indeces not validated.
*/
function updateHeader(cellsNotValidated) {
for (var i = 0; i <= headers.length - 1; i++) {
// if i is in cellsNotValidated, it has to be red;
var cellHeaderSelector = $(headers[i]);
var cellNotValidated = cellsNotValidated.indexOf(i) >= 0;
if (cellNotValidated) {
headers[i] = cellHeaderSelector.addClass('notValidated');
} else {
headers[i] = cellHeaderSelector.removeClass('notValidated');
}
};
// make sure to update your hot instance to trigger the re-render the grid
hotInstance.updateSettings({
colHeaders: headers
});
}
并排绘制两个图:
mfrow
答案 1 :(得分:4)
答案 2 :(得分:1)
我知道您正在寻找基础R解决方案。真遗憾。 ggplot2为这些任务提供了很多可能性:
data3 = data.frame(data_nr = rep(c("data1","data2"), each=15),
cat_1 = rep(rep(letters[1:5], each=3),2),
cat_2 = rep(rep(LETTERS[1:3], 5),2),
height = c(1:5, rep(1,5), rep(4,5), 2:6, rep(2,5), rep(3,5)))
ggplot(data3, aes(x=cat_1, y=height, fill=cat_2)) +
geom_bar(stat="identity", position="stack") +
facet_grid(~ data_nr)
我可以想到一个(不那么优雅)纯粹的基础R解决方案,使用一点点黑客:
data12 = cbind(data1, rep(NA,3), data2)
barplot(data12, space=0)
答案 3 :(得分:1)
这是自定义函数,您可以在组图之间和组图之间进行操作,并且可以使用您想要绘制的任意数量的数据来完成。
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, ...)
}
以下是三个数据集的示例。请注意,所有数据都具有相同的行数,但列不同。当行值被堆叠时,应该是相同的数字,但是可以通过将NA或0设置为缺少的组来使行相等。
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")
答案 4 :(得分:-2)
library(gridExtra)
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)
# stacked bar
plot1 <- barplot(data1)
#grouped var
plot2<- barplot(data1, beside=TRUE)
grid.arrange(plot1, plot2, ncol=2)