BAR GRAPHS
In this lesson you will learn how to plot bar graphs in R. Load the data HERE into a table named data. Name the columns subject, group, and rt.
If you simply type in barplot(data$rt) you will get a bar plot of data$rt with a column representing each data point. There is a lot you can do with a bar plot in R, so lets work through some of the basics.
Let's say you wanted to colour the bars in green, you would first need the colour name in R. You can find a guide HERE.
Try this. barplot(data$rt, col = 'forestgreen')
Alternatively, use barplot(data$rt, col = 'forestgreen', border = NA) if you do not want borders on the bars.
If you wanted to color subsets of the bars, you would have to specify colors for each bar. You could do something like this:
cols = data$subject
cols[1:50] = c("red")
cols[51:100] = c("blue")
cols[101:150] = c("green")
barplot(data$rt, col = cols)
Some other useful features:
To label the x axis:
barplot(data$rt,xlab = 'Subject')
To label the y axis:
barplot(data$rt,ylab = 'Reaction Time (ms)')
To add a title:
barplot(data$rt,main = 'Insert Title Here')
To add a title and a subtitle:
barplot(data$rt,main = 'Insert Title Here', sub = 'My Subtitle')
To set x limits to the x axis:
barplot(data$rt, xlim = c(1, 50))
And to set y limits to the y axis:
barplot(data$rt, ylim = c(150, 550))
A full list of the parameters for barplot can be found HERE.
Plotting Means With Error Bars
A common thing you may want to do with this data is to generate a bar plot of the means of the three groups with an error bar for each column.
Step One. Get the mean for each group:
means = aggregate(data$rt,list(data$group),mean)
N.B., for simplicities sake, I am going to remove the means from the mini data frame they have been put in:
means = means$x
Step Two. Get the standard deviation for each group.
sds = aggregate(data$rt,list(data$group),sd)
sds = sds$x (same removal of the data frame here)
Step Three. Compute the CI size for each group.
cis = abs(qt(0.05,49) * sds / sqrt(50))
Step Four. Plot the bar plot of the means.
bp = barplot(means, ylim = c(200,400))
Note, we are assigning the barplot a name, bp, so we can use it below.
Step Five. Add the error bars.
arrows(bp,means-cis, bp, means+cis, angle=90, code=3, length=0.5)
This is actually a quick R cheat using the arrows command, but it works! Essentially, you are specifying arrows draw between the point (bp,means-cis) and point (bp,means+cis) with the arrows heads at an angle of 90 degrees, on each end of the line (code = 3) and with a length of 0.5. If you want to get serious about plotting, use a plotting program or see some of the more advanced lessons!
If you simply type in barplot(data$rt) you will get a bar plot of data$rt with a column representing each data point. There is a lot you can do with a bar plot in R, so lets work through some of the basics.
Let's say you wanted to colour the bars in green, you would first need the colour name in R. You can find a guide HERE.
Try this. barplot(data$rt, col = 'forestgreen')
Alternatively, use barplot(data$rt, col = 'forestgreen', border = NA) if you do not want borders on the bars.
If you wanted to color subsets of the bars, you would have to specify colors for each bar. You could do something like this:
cols = data$subject
cols[1:50] = c("red")
cols[51:100] = c("blue")
cols[101:150] = c("green")
barplot(data$rt, col = cols)
Some other useful features:
To label the x axis:
barplot(data$rt,xlab = 'Subject')
To label the y axis:
barplot(data$rt,ylab = 'Reaction Time (ms)')
To add a title:
barplot(data$rt,main = 'Insert Title Here')
To add a title and a subtitle:
barplot(data$rt,main = 'Insert Title Here', sub = 'My Subtitle')
To set x limits to the x axis:
barplot(data$rt, xlim = c(1, 50))
And to set y limits to the y axis:
barplot(data$rt, ylim = c(150, 550))
A full list of the parameters for barplot can be found HERE.
Plotting Means With Error Bars
A common thing you may want to do with this data is to generate a bar plot of the means of the three groups with an error bar for each column.
Step One. Get the mean for each group:
means = aggregate(data$rt,list(data$group),mean)
N.B., for simplicities sake, I am going to remove the means from the mini data frame they have been put in:
means = means$x
Step Two. Get the standard deviation for each group.
sds = aggregate(data$rt,list(data$group),sd)
sds = sds$x (same removal of the data frame here)
Step Three. Compute the CI size for each group.
cis = abs(qt(0.05,49) * sds / sqrt(50))
Step Four. Plot the bar plot of the means.
bp = barplot(means, ylim = c(200,400))
Note, we are assigning the barplot a name, bp, so we can use it below.
Step Five. Add the error bars.
arrows(bp,means-cis, bp, means+cis, angle=90, code=3, length=0.5)
This is actually a quick R cheat using the arrows command, but it works! Essentially, you are specifying arrows draw between the point (bp,means-cis) and point (bp,means+cis) with the arrows heads at an angle of 90 degrees, on each end of the line (code = 3) and with a length of 0.5. If you want to get serious about plotting, use a plotting program or see some of the more advanced lessons!