An alternative approach using eigenfunctions R:
#Taking the Dataset mentioned in the question df = data.frame(Block = c("Block 1", "Block 2", "Block 3", "Block 4", "Block 5"), RTreg = c(0.0,667.208,645.47,674.52,651.42), RTrnd = c(862.07,770.43,696.02,659.476,633.733)) #Create a Matrix which will help in creating the plot value_matrix = matrix(, nrow = 2, ncol = 5) #An empty matrix is a necessary requirement prior to copying data value_matrix[1,] = df$RTreg value_matrix[2,] = df$RTrnd #Note that the "beside" argument has to be kept "TRUE" in order to place the bars side by side barplot(value_matrix, names.arg = df$Block, beside = TRUE, col = c("peachpuff", "skyblue"), legend.text = c("RTreg", "RTrnd"))
What happens above?
To answer this question, I would first like to draw your attention to the first argument of barplot() i.e. 'height'. Now, if you provide a vector as input, the function will generate a regular histogram, as you would expect. However, to build a clustered histogram, you will need to provide data in a matrix format. Now here comes in magic. For a clustered histogram, your variables should be present in the rows of the matrix, and not in ordinary columns. I want to say, instead of storing data column by column:
RTreg RTrnd 1 0.0000 862.0707 2 667.2081 770.4315 3 645.4730 696.0200
save data line by line:
1 2 3 RTreg 0.0000 667.2081 645.4730 RTrnd 862.0707 770.4315 696.0200
This is how barplot() recognizes 2 different variables "RTreg" and "RTrnd". If you have m other variables that you want to put into the cluster, just copy them into the matrix rows after setting the nrow matrix() argument to m. Now, to get the above structure, I just created an empty matrix and copied “RTreg” and “RTrnd” to lines 1 and 2, respectively. Once the matrix is created, then you are ready. Just call barplot() along with your matrix as an argument to the height and remember to set the argument next to TRUE.
The result of the above code:
