交错列

时间:2013-01-08 20:33:14

标签: r bar-chart

我正在尝试在R中生成类似于

的数字

![](http://imgur.com/Srt9L_png)

包含来自两个不同数据集的数字的列交错。我完成了大部分困难的工作,即使用此代码

library(gplots)
meang7 <- c(355.8303, 241.9443, 253.2399, 356.4712, 779.6908, 1444.0087, 1215.7648)
meang4 <- c(196.97559, 185.70940, 190.80855, 165.82834, 190.64914, 152.46845, 85.21208)
SEMg7 <- c(20.13935, 20.74347, 19.35914, 28.60324, 53.02302, 84.71020, 86.93199)
SEMg4 <- c( 9.469664, 7.515927, 12.500072, 10.379942, 4.455329, 15.124991, 6.888774)
barg7 <- barplot(meang7, main="C. Sapidus 35-22 ppt",     names.arg=c(0,6,12,24,48,96,168),ylim=c(0,1700), col="gray", axis.lty=1, xlab="Time Post     Transfer (hr)", ylab="CA Activity (μmol CO2 mg protein−1 min−1)")
barg4 <- barplot(meang4, col="blue", add=TRUE)
plotCI(x=barg7, y=meang7, uiw=SEMg7, lty=1, gap=0, add=TRUE)
plotCI(x=barg4, y=meang4, uiw=SEMg4, lty=1, gap=0, add=TRUE)

获取

到目前为止 enter image description here

但我无法弄清楚如何使列交错而不是重叠。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

在这里。您需要按行将两个向量合并为一个矩阵,并使用beside的{​​{1}}参数:

barplot()

答案 1 :(得分:0)

首先,您需要使用reshape包将数据重新整形为更有用的长格式。

library(reshape)
library(ggplot2)
meang7 <- c(355.8303, 241.9443, 253.2399, 356.4712, 779.6908, 1444.0087, 1215.7648)
meang4 <- c(196.97559, 185.70940, 190.80855, 165.82834, 190.64914, 152.46845, 85.21208)
SEMg7 <- c(20.13935, 20.74347, 19.35914, 28.60324, 53.02302, 84.71020, 86.93199)
SEMg4 <- c( 9.469664, 7.515927, 12.500072, 10.379942, 4.455329, 15.124991, 6.888774)
hours <- as.factor(c(0,6,12,24,48,96,168))
d <- data.frame(m7=meang7, m4=meang4, hours=hours )
d <- melt(d, measure.vars=c("m7", "m4"))
d$se <- c(SEMg7, SEMg4)

使用标准错误创建误差线的限制并使用ggplot创建绘图:

limits <- aes(ymax=d$value + d$se, ymin=d$value - d$se)
ggplot(d, aes(x=hours, y=value, fill=variable)) + geom_bar(stat="identity", position="dodge") + geom_errorbar(ymax=d$value + d$se, ymin=d$value-d$se) + scale_y_continuous(limits=c(0, 1600))