14.3 Exercise 8: Base plots

Create the script “exercise8.R” and save it to the “Rcourse/Module3” directory: you will save all the commands of exercise 8 in that script.
Remember you can comment the code using #.

Answer
getwd()
setwd("~/Rcourse/Module3")

14.3.1 Exercise 8a- scatter plot

1- Create the following data frame

genes <- data.frame(sample1=rnorm(300),
                    sample2=rnorm(300))

2- Create a scatter plot showing sample1 (x-axis) vs sample2 (y-axis) of genes.

Answer
plot(genes$sample1, genes$sample2)

3- Change the point type and color.

Answer
plot(genes$sample1, 
     genes$sample2, 
     col="lightblue", 
     pch=3)

4- Change x-axis and y-axis labels to “Sample 1” and “Sample 2,” respectively.

Answer
plot(genes$sample1, 
     genes$sample2, 
     col="lightblue", 
     pch=3,
     xlab="Sample 1", 
     ylab="Sample 2")

5- Add a title to the plot.

Answer
plot(genes$sample1, 
     genes$sample2, 
     col="lightblue", 
     pch=3,
     xlab="Sample 1", 
     ylab="Sample 2",
     main="scatter plot")

6- Add a vertical red line that shows the median expression value of sample 1. Do it in two steps:
a. calculate the median expression of genes in sample 1.
b. plot a vertical line using abline().

Answer
# median expression of sample1
med1 <- median(genes$sample1)

# plot
plot(genes$sample1, 
     genes$sample2, 
     col="lightblue", 
     pch=3,
     xlab="Sample 1", 
     ylab="Sample 2",
     main="scatter plot")

# vertical line
abline(v=med1, col="red")

14.3.2 Exercise 8b- bar plot + pie chart

1- Create the following vector

genes_significance <- rep(c("enriched", "depleted", "none"), c(20, 32, 248))

2- The vector describes whether a gene is up- (enriched) or down- (depleted) regulated, or not regulated (none).
Produce a barplot that displays this information: how many genes are enriched, depleted, or not regulated.

Answer
barplot(table(genes_significance))

3- Color the bars of the boxplot, each in a different color (3 colors of your choice)

Answer
barplot(table(genes_significance), 
  col=c("blue", "red", "grey"))

# If you want to order the bars not by the default (alphabetical) order, you need to create an ordered factor!
genes_factor <- factor(genes_significance, ordered=TRUE, levels=c("enriched", "none", "depleted"))
barplot(table(genes_factor), 
  col=c("blue", "red", "grey"))

4- Use the argument “names.arg” in barplot() to rename the bars: Change depleted to “Down,” enriched to “Up,” none to “Not significant”

Answer
barplot(table(genes_significance), 
  col=c("blue", "red", "grey"), 
  names.arg=c("Down", "Up", "Not significant"))

5- The “las” argument allows to rotate the x-axis labels for a better readability. Try value 2 for las: what happens?

Answer
barplot(table(genes_significance), 
  col=c("blue", "red", "grey"), 
  names.arg=c("Down", "Up", "Not significant"), 
  las=2)

6- Create a pie chart of the same information (Enriched, Depleted, None)

Answer
pie(table(genes_significance))

Change the color of the slices, modify the labels, and add a title.

Answer
pie(table(genes_significance), 
    col=c("blue", "red", "grey"), 
    main="pie chart", 
    labels=c("Down", "Up", "Not significant"))

14.3.3 Exercise 8c- histogram

1- Use genes object from exercise 11a to create a histogram of the gene expression distribution of sample 1.

Answer
hist(genes$sample1)

2- Repeat the histogram but change argument “breaks” to 50.
What is the difference ?

Answer
hist(genes$sample1, 
  breaks=50)

3- Color this histogram in light blue.

Answer
hist(genes$sample1, 
  breaks=50, 
  col="lightblue")

4- “Zoom in” the histogram: show only the distribution of expression values from 0 to 2 (x-axis) using the xlim argument.

Answer
hist(genes$sample1, 
  breaks=50, 
  col="lightblue",
  xlim=c(0, 2))

5- Save the histogram in a pdf file.

Answer
pdf("myhistogram.pdf")

hist(genes$sample1, 
    breaks=50, 
    col="lightblue", 
    xlim=c(0, 2))

dev.off()