Bar Chart with R: a demo
It’s very common to visualize comparison data sets with bar chart. Below is such a data set stored in a file, named “benchmark-result.csv” for example.
System Benchmarks Index Values, HPCloud, AWS-Micro Dhrystone 2 using register variables, 2315.6, 782.1 Double-Precision Whetstone, 578.9, 195.3 Execl Throughput, 724.0, 48.1 File Copy 1024 bufsize 2000 maxblocks, 1515.5, 260.8 File Copy 256 bufsize 500 maxblocks, 991.5, 169.4 File Copy 4096 bufsize 8000 maxblocks, 2321.8, 599.4 Pipe Throughput, 1031.0, 68.4 Pipe-based Context Switching, 506.9, 35.2 Process Creation, 770.3, 60.6 Shell Scripts (1 concurrent), 1024.6, 114.1 Shell Scripts (8 concurrent), 973.5, 103.7 System Call Overhead, 1235.3, 56.0 System Benchmarks Index Score , 1044.2, 127.7
Now launch to R shell, and run following scripts:
- Load data:
benchmark = read.table('/home/james/benchmark-result.csv', header=TRUE, sep=',')
- Create matrix:
benchmark.matrix = as.matrix(benchmark[2:3])
- Set row names:
rownames(benchmark.matrix) <- benchmark[,1]
- Open graphics device for PNG:
png(filename="/home/james/benchmark-result-barchart.png", width=800, height=520, unit="px")
- Generate the bar chart:
barplot(t(benchmark.matrix), beside=TRUE, legend=TRUE, ylab="Index Value", cex.names=.75)
- Close graphics device:
dev.off()
Done.