Advertisement
otorp2

side by side barplot

Jul 11th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. https://stats.stackexchange.com/questions/3842/how-to-create-a-barplot-diagram-where-bars-are-side-by-side-in-r
  2.  
  3.  
  4. accepted
  5. I shall assume that you are able to import your data in R with read.table() or the short-hand read.csv() functions. Then you can apply any summary functions you want, for instance table or mean, as below:
  6.  
  7. x <- replicate(4, rnorm(100))
  8. apply(x, 2, mean)
  9. or
  10.  
  11. x <- replicate(2, sample(letters[1:2], 100, rep=T))
  12. apply(x, 2, table)
  13. The idea is to end up with a matrix or table for the summary values you want to display.
  14.  
  15. For the graphical output, look at the barplot() function with the option beside=TRUE, e.g.
  16.  
  17. barplot(matrix(c(5,3,8,9),nr=2), beside=T,
  18. col=c("aquamarine3","coral"),
  19. names.arg=LETTERS[1:2])
  20. legend("topleft", c("A","B"), pch=15,
  21. col=c("aquamarine3","coral"),
  22. bty="n")
  23. The space argument can be used to add an extra space between juxtaposed bars.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement