Barplot with two variables next to each other

I am trying to get a barplot, which is quite common, but despite reading a ton of documentation on plotting graphs in R and documentation on ggplot and all its layers, I cannot get this graph the way I want it to.

My data is pretty simple.

aov.data Block RTreg RTrnd 1 Block1 0.0000 862.0707 2 Block2 667.2081 770.4315 3 Block3 645.4730 696.0200 4 Block4 674.5200 659.4765 5 Block5 651.4295 633.7333 

What I want to get is a bar chart with a Block column on the x axis, working as a categorical variable. On the y-axis, I want to get the values ​​for building the columns RTreg and RTrnd . This is the part that I cannot understand correctly. What I would like is to have two bars per X axis mark. One column representing the RTreg value in this block, and one column representing the RTrnd value in this block. More or less like this:

Valid XHTML .

But with 5 sets of two bars instead of two.

So far I have used the following code:

 ggplot(aov.data,aes(x=Block)) + geom_bar(aes(y=RTreg),stat="identity",position="dodge",col="blue") + geom_bar(position="dodge") + geom_bar(aes(y=RTrnd),stat="identity",position="dodge",col="red") 

I thought that at first I had to create a basic graph with a categorical x axis containing blocks. Then, with geom_bar I thought that I was adding the RTreg column RTreg , and with the second geom_bar I was adding the RTrnd column. With the position option equal to dodge , I was going to get two bars side by side. However, the plot I get:

Valid XHTML .

Any idea on how to get two bars side by side, and hopefully with different colors and a legend for each? I would really appreciate some guidance here.

Thanks in advance.

+10
r plot ggplot2
source share
2 answers

You have to redo your data frame from wide to long, and then you do not need to set the bars for each condition separately.

Assuming the data frame is named df .

 library(reshape2) df.long<-melt(df) ggplot(df.long,aes(Block,value,fill=variable))+ geom_bar(stat="identity",position="dodge") 

enter image description here

+24
source share

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:

enter image description here

+1
source share

All Articles