Just a short post on making a barplot in R after reading in data via the read.table() function. I created a file with two rows, the first row containing the header and the second row containing the data values.
a b c d e 10 20 30 20 10
x <- read.table("some.file")
#the data needs to be in matrix format for barplot()
barplot(as.matrix(x[2,]))
#to label the x-axis
barplot(as.matrix(x[2,]),names.arg=as.matrix(x[1,]))
#to label the x-axis using horizontal labels
barplot(as.matrix(x[2,]),names.arg=as.matrix(x[1,]),las=2)
#export as postscript
postscript(file="some_file.ps")
barplot(as.matrix(x[2,]),names.arg=as.matrix(x[1,]),las=2)
dev.off()
Horizontal barplot sorted by values
x <- matrix(sample(100,10),ncol=10,nrow=1)
x
a b c d e f g h i j
[1,] 33 92 99 15 53 24 62 27 58 72
colnames(x) <- c('a','b','c','d','e','f','g','h','i','j')
x <- x[,order(x)]
x
d f h a e i g j b c
15 24 27 33 53 58 62 72 92 99
barplot(x, horiz=T, las=1)

This work is licensed under a Creative Commons
Attribution 4.0 International License.

