R中的条形图帮助

问题描述:

我试图设置一个条形图来比较特定化合物的控制和实验样本。该数据集被称为“hydrocarbon3”,并包含以下信息:R中的条形图帮助

Exp. Contr. 
c12 89 49 
c17 79 30 
c26 78 35 
c42 63 3 
pris 0.5 0.8 
phy 0.5 0.9 
nap 87 48 
nap1 83 44 
nap2 78 44 
nap3 73 20 
acen1 81 50 
acen2 86 46 
fluor 83 11 
fluor1 68 13 
fluor2 79 17 
dibe 65 7 
dibe1 67 6 
dibe2 56 10 
phen 82 13 
phen1 70 12 
phen2 65 15 
phen3 53 14 
fluro 62 9 
pyren 48 11 
pyren1 34 10 
pyren2 19 8 
chrys 22 3 
chrys1 21 3 
chrys2 21 3 

当我创建与式的条形图:

barplot(as.matrix(hydrocarbon3), 
     main=c("Fig 1. Change in concentrations of different hydrocarbon compounds\nin sediments with and without the presence of bacteria after 21 days"), 
     beside=TRUE, 
     xlab="Oiled sediment samples collected at 21 days", 
     space=c(0,2), 
     ylab="% loss in concentration relative to day 0") 

我接收该图中,但我所需要的控制和每种化学物质的实验样品彼此相邻允许进行更准确的比较,而不是左侧的实验样品和右侧的对照样品:有没有办法在R上进行校正? barchart

尝试调换你的矩阵:

barplot(t(as.matrix(hydrocarbon3)), beside=T) 

基本上,barplot将绘制的东西在他们的矩阵,由于矩阵仅仅是一个载体包裹colwise显示顺序,意味着barplot将绘制所有第一列的值,那么所有这些第二列等

检查这个问题了:Barplot with 2 variables side by side

它采用GGPLOT2,所以你必须使用日e运行之前的代码如下:

intall.packages("ggplot2") 
library(ggplot2) 

希望这对你有效。加上它看起来更好用ggplot2!

> df 
    row exp con 
1 a 1 2 
2 b 2 3 
3 c 3 4 
> barplot(rbind(df$exp,df$con), 
+   beside = TRUE,names.arg=df$row) 

生产:

enter image description here