我想创建一个组合条形图,其中 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)
知道我该怎么做吗?我知道我们可以使用不同的图或方面来做到这一点,但我想在一个图中并排呈现它们。
您可以使用参数space
of barplot
to 移动第二个条形图:
# plot first barplot, using xlim to ensure there is enough space to plot the second one
# (here the spaces are all 0, so the bars are grouped together)
barplot(data1, space=0, xlim=c(-1, ncol(data1)+ncol(data2)+1), las=1)
# plot second barplot (with add=TRUE so the bars appear on the same plot),
# putting a space of 6 (the number of columns of data1 + 1) at the beginning,
# to plot the "new" bars on the right of the first bars (the other spaces are 0):
barplot(data2, space=c(ncol(data1)+1, rep(0, ncol(data2)-1)), las=1, add=TRUE)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)