These exercises cover base plotting for Plotting in R.

Exercise 1

Read in the data “timecourse.csv” as a data.frame and get a summary of column values

timeCourse <- read.delim("./data/timecourse.csv",sep=",",header = T) 
summary(timeCourse)
##       Time         Control1        Control2       Treatment1      Treatment2  
##  Min.   : 0.0   Min.   :2.000   Min.   :3.000   Min.   : 3.00   Min.   : 4.0  
##  1st Qu.: 2.5   1st Qu.:3.500   1st Qu.:3.500   1st Qu.: 7.00   1st Qu.: 8.5  
##  Median : 5.0   Median :5.000   Median :4.000   Median :12.00   Median :13.0  
##  Mean   : 5.0   Mean   :4.909   Mean   :4.727   Mean   :10.36   Mean   :11.0  
##  3rd Qu.: 7.5   3rd Qu.:6.500   3rd Qu.:6.000   3rd Qu.:14.50   3rd Qu.:14.0  
##  Max.   :10.0   Max.   :7.000   Max.   :7.000   Max.   :15.00   Max.   :15.0
timeCourse <- read.delim("./data/timecourse.csv",sep=",",header = T) 
summary(timeCourse)
##       Time         Control1        Control2       Treatment1      Treatment2  
##  Min.   : 0.0   Min.   :2.000   Min.   :3.000   Min.   : 3.00   Min.   : 4.0  
##  1st Qu.: 2.5   1st Qu.:3.500   1st Qu.:3.500   1st Qu.: 7.00   1st Qu.: 8.5  
##  Median : 5.0   Median :5.000   Median :4.000   Median :12.00   Median :13.0  
##  Mean   : 5.0   Mean   :4.909   Mean   :4.727   Mean   :10.36   Mean   :11.0  
##  3rd Qu.: 7.5   3rd Qu.:6.500   3rd Qu.:6.000   3rd Qu.:14.50   3rd Qu.:14.0  
##  Max.   :10.0   Max.   :7.000   Max.   :7.000   Max.   :15.00   Max.   :15.0

Exercise 2

Produce a histogram of the values for control and treatment samples

hist(as.matrix(timeCourse)[,2:3],main = "Control",xlab = "Control")

hist(as.matrix(timeCourse)[,4:5],main = "Treatment",xlab = "Treatment")

Exercise 3

Put these in the same plot with same X scale limits

par(mfrow=c(2,1))
hist(as.matrix(timeCourse)[,2:3],xlim = c(0,20),main = "Control",xlab = "Control")
hist(as.matrix(timeCourse)[,4:5],xlim = c(0,20),main = "Treatment",xlab = "Treatment")

Exercise 4

Make a barplot of values in control sample 1 for every time point

par(mfrow=c(1,1))

control1 <- timeCourse[,2]
barplot(control1,names.arg = timeCourse[,1],xlab = "Time")

Exercise 5

Make a barplot of mean values in control and treatment samples side by side for every time point

par(mfrow=c(1,1))

controlMeans <- rowMeans(timeCourse[,c(2,3)])
treatmentMeans <- rowMeans(timeCourse[,c(4,5)])
meanTable <- rbind(controlMeans,treatmentMeans)
barplot(meanTable,names.arg = timeCourse[,1],xlab = "Time",beside=TRUE,col=c("blue","red"))

legend("topleft",legend = c("Control","Treatment"),fill=c("blue","red"))

Exercise 6

Create a line graph showing control and treatment samples against time.

topOfY <- max(timeCourse[,-1])+3
 
plot(timeCourse[,2],type="o",xlab="Time",ylab="Score",pch=1,ylim=c(0,topOfY),col="blue") 
lines(timeCourse[,3],pch=1,col="blue",type="o") 
lines(timeCourse[,4],pch=1,col="red",type="o") 
lines(timeCourse[,5],pch=1,col="red",type="o") 
legend("topleft",legend = c("Control","Treatment"),col=c("blue","red"),lwd = 2)