3A. PLOTTING BASICS
In this lesson we will review the basics of plotting data in R. Some of this is covered in other lessons and some of it is not. For now, load the data file HERE into a table called "data".
Next, name the three columns "subject", "condition", and "rt". You will note there are three groups of 50 participants in this data set.
Let's generate a basic scatter plot: plot(data$rt)
You will note this command generates a scatter plot of the values in data$rt with subject as the x axis value and rt as the y axis value.
If you wanted to represent this data as a bar plot you would simply use: barplot(data$rt)
For now, let's return to a basic scatter plot: plot(data$rt)
If you wanted to visualize the group differences, you could use a plot like this: plot(data$rt~data$condition)
This essentially tells R that you want a scatter plot of data$rt but instead of data$subject on the x axis you would like to use data$condition. Try plot(data$rt~data$subject) as a proof of concept. The default in R for the plot command is to assume the x axis reflects each data point if nothing else is specified. Note, this will not work for barplots - you need to generate the means in advance. See below.
Fitting a regression line to a scatter plot in R is easy. Try: abline(lm(data$rt~data$subject))
This command adds a regression line (a linear model: lm) for data$rt as a function of data$subject.
You could simply construct the regression line (linear model) for the condition effect by stating:
plot(data$rt~data$condition)
abline(lm(data$rt~data$condition))
You can add other information to your plot. For example, if you want to label the plot axes in R you would do the following:
plot(data$rt~data$condition, main="Condition Effect", xlab="Condition", ylab="Reaction Time (ms)", ylim=c(0, 500))
Go HERE for a lot more information information about what you can add to plots.
We can make our graph even more sophisticated. Let's highlight the means for each group.
means = aggregate(rt~condition,data,mean)
This command gets the mean values for each condition in the table "data" and assigns them to the variable means. Type "means" to check.
Now try: points(means$condition,means$rt,pch=16,col='red')
You should see a red circle reflecting the mean in the middle of each scatter plot for condition.
A different way to visualize this data would be to plot each group separately using a line to reflect the mean as per a previous assignment. You would do this as follows:
par(mfrow=c(1,3))
plot(data$rt[data$condition==1])
abline(a = means$rt[1], b = 0, col = "red")
plot(data$rt[data$condition==2])
abline(a = means$rt[2], b = 0, col = "red")
plot(data$rt[data$condition==3])
abline(a = means$rt[3], b = 0, col = "red")
More details on plotting in R can be found HERE and HERE.
Assignment Question
1. Using a data set of your choice generate a plot similar to the one immediately above. Ensure you title the plot, label the axes, and make sure the x and y limits are the same on all of your sub plots.