1

I am trying to set up a bar chart to compare control and experimental samples taken of specific compounds. The data set is known as 'hydrocarbon3' and contains the following information:

    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

When I create a bar chart with the formula:

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")

I receive this diagram, however I need the control and experimental samples of each chemical be next to each other allow a more accurate comparison, rather than the experimental samples bunched on the left and control samples bunched on the right: Is there a way to correct this on R? barchart

zx8754
  • 52,746
  • 12
  • 114
  • 209
user3170629
  • 489
  • 3
  • 12
  • 21

3 Answers3

0

Try transposing your matrix:

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

Basically, barplot will plot things in the order they show up in the matrix, which, since a matrix is just a vector wrapped colwise, means barplot will plot all the values of the first column, then all those of the second column, etc.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
0

Check this question out: Barplot with 2 variables side by side

It uses ggplot2, so you'll have to use the following code before running it:

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

Hopefully this works for you. Plus it looks a little nicer with ggplot2!

Community
  • 1
  • 1
Ford
  • 2,439
  • 4
  • 17
  • 15
0
> 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)

produces:

enter image description here

TYZ
  • 8,466
  • 5
  • 29
  • 60