Home > database >  Creating a barplot from csv file
Creating a barplot from csv file

Time:09-12

I'm trying to accomplish a pretty simple task but I am really struggling to achieve what I want. I created a csv file for my data set and am now trying to import it into R and create a barplot from it. This is what the data set looks like.

control salted unsalted
3 12 23
7 15 25
4 13 20
2 17 18

From this, I want to create an error bar chart but I can't figure out how to make it work. I don't think I should need a package for this but am I mistaken?

barplot, as requested

CodePudding user response:

barplot returns the x coordinates of bar midpoints so you can use these to add other stuff to the plot using e.g. segments:

# borrowed from Dubukay's answer
dat <- data.frame(
  control=c(3, 7, 4, 2),
  salted=c(12, 15, 13, 17),
  unsalted=c(23, 25, 20, 18)
)

# calculate mean and SD for each treatment
means <- sapply(dat, mean)
sds <- sapply(dat, sd)

bp <- barplot(means, ylim=c(0, max(means   sds)))
# vertical lines
segments(bp, means - sds, y1=means   sds)
# horizontal bars
bar.wd <- .3
segments(rep(bp, 2) - bar.wd/2, 
         means   c(-sds, sds), 
         rep(bp, 2)   bar.wd/2)

Barplot with error bars

  • Related