You need to have these files and variables: Input data; Read file with read counts

Counts=read.table(file="Dsim_count_table.tsv", header=T, row.names=1)

Keep those genes that were expressed in at a reasonable level (25 pairs) in all samples

K=Counts[rowSums(Counts>=25)==12, ]

log10 K

L=log10(K)

color vector

Color_4for12=rep(c("blue", "cyan", "orange", "red"), each=3)

Legend vector

Leg=c("C15", "C23", "W15", "W23")

Exercises from previous lesson

Task 2

Create a density plot from all of the samples in counts in log10 scale with colored lines:

c15 | light blue

c23 | bark blue

w15 | light red

w23 | dark red

You can try the RColorBrewer Paired palette as well, or use Color_4for12.

Solution 2 from Fazekas Dávid:

Counts=read.table(file="Dsim_count_table.tsv", header=T, row.names=1)

K=Counts[rowSums(Counts>=25)==12, ]

L=log10(K)

Color_4for12=rep(c("light blue", "dark blue", "pink", "red"), each=3)

Leg=c("C15", "C23", "W15", "W23")

png("task_2.png")

  plot(NA, xlim=c(1,6), ylim=c(0,1), xlab="Log10 counts", ylab="Density", main="Density plots")
  
  for( i in 1:12) {
    lines(density(L[,i]), col=Color_4for12[i])
  }
  
  legend("topright", legend=Leg, lwd=2, col=c("light blue", "dark blue", "pink", "red"), bty="n")
  
dev.off()
## png 
##   2

Task 1

Create a boxplot diagram from the counts in log10 scale. Colour it with the colour scheme used in density plots (You can use Color_4for12 with these colours -blue, -cyan, -orange, -red). Make the x-axes label to horizontal or 45° angle. Save the picture as a png file.

Solution 1

Color_4for12=rep(c("blue", "cyan", "orange", "red"), each=3)

boxplot(L, col=Color_4for12, xaxt="n")

axis(1, at=1:12, labels=colnames(L), las=2) # vertical

boxplot(L, col=Color_4for12, xaxt="n")

text(seq(1, 12, by=1), par("usr")[3] - 0.2, labels = colnames(L), srt = 45, pos = 2, xpd = TRUE)

Task 3

Try to find a solution to fill a colour the area under the curve of a density plot. Create a density plot for C15_r1 sample (log10 scale) with coloured under curve area.

Solution 3

Counts=read.table("Dsim_count_table.tsv", header=T, row.names=1)

K=Counts[rowSums(Counts>=25)==12, ]

L=log10(K)

d = density(L$C15_r1)

plot(density(L$C15_r1))

polygon(d, col="blue", border="black")

1. Creating nice colors with RColorbrewer library.

library(RColorBrewer)

Show all the color schemes available.

display.brewer.all()

View a single RColorBrewer palette by specifying the number of colors and its name.

display.brewer.pal(9,"Set1")

Save their code to a vector.

Color_1=brewer.pal(9,"Set1")

2. Coloring point of a scatterplot according to their density.

Plotting the log counts of C23_r1 (x axis) and C23_r2 (y axis). Use diamond mark. Give a title for the figure. Do not save the png result files!

plot(L[, 4:5], pch=18, main="C23_plot")

Creating a vectors of chosen colors. (I chose purple-pink “PuRD”)

display.brewer.pal(9,"PuRd")[3:9]

## NULL

ColorRampPalette interpolate a set of given colors to create new color ramps.

Col_purd=colorRampPalette(brewer.pal(9,"PuRd")[3:9])

densCols produces a vector containing colors which encode the local densities at each point in a scatterplot.

Col_densi=densCols(L$C23_r1, L$C23_r2, colramp=Col_purd)

plot(L[, 4:5], col=Col_densi, pch=18, main="C23_plot")

3. Adding lines to the plot

Read the file contains the results of differential expression analysis of C23 vs W23.

R=read.table("C23vsW23_results.tsv", header=T)

Add a column contains Benjamini-Hochberg corrected p-values.

 R$adj.PValue = p.adjust(R$PValue, method="BH")

Plot the density of corrected p-values and add a red vertical line at the level of 0.05 (significant results).

plot(density(R$adj.PValue))

abline(v=0.05, col="red")

4.Combining plots

Introduction about combining plots:

http://www.statmethods.net/advgraphs/layout.html R makes it easy to combine multiple plots into one overall graph, using either the par or layout functions.

par(mfrow=c(nrows, ncols))

Plot the density of logFC and adjusted p-values to 2 separate plots on the same figure,they should lay under one under.

par(mfrow=c(2,1))

plot(density(R$logFC), main="logFC", cex.axis=0.6, cex.lab=0.6, cex.main=0.6)

plot(density(R$adj.PValue), main="BH corrected P-val", cex.axis=0.6, cex.lab=0.6, cex.main=0.6)

abline(v=0.05, col="red")

Task for lesson

Put the two plots side by side. (The plots do not be under one under.)

Try to plot a single plot again. Set back R to plot 1 plot per figure again.

par(mfrow=c(1,1))

plot(density(R$logFC), main="logFC", cex.axis=0.6, cex.lab=0.6, cex.main=0.6)

plot(density(R$adj.PValue), main="BH corrected P-val", cex.axis=0.6, cex.lab=0.6, cex.main=0.6)

abline(v=0.05, col="red")

5. Changing font types on a plot

Windows

windowsFont()

plot(1:10,1:10,type=“n”) # another way to create an empty

plot: type=“n”

windowsFonts(

A=windowsFont(“Arial Black”),

B=windowsFont(“Bookman Old Style”),

C=windowsFont(“Comic Sans MS”),

D=windowsFont(“Symbol”)

)

text(3,3,“Hello World Default”)

text(4,4,family=“A”,“Hello World, Arial Black”)

text(5,5,family=“B”,“Hello World, Bookman Old Style”)

text(6,6,family=“C”,“Hello World, Comic Sans MS”)

text(7,7,family=“D”, “Hello World, Symbol”)

Linux

It depends on the installed fonts.

Fonts=names(pdfFonts())

Fonts

[1] “serif” “sans” “mono”

[4] “AvantGarde” “Bookman” “Courier”

[7] “Helvetica” “Helvetica-Narrow” “NewCenturySchoolbook”

[10] “Palatino” “Times” “URWGothic”

[13] “URWBookman” “NimbusMon” “NimbusSan”

[16] “URWHelvetica” “NimbusSanCond” “CenturySch”

[19] “URWPalladio” “NimbusRom” “URWTimes”

[22] “ArialMT” “Japan1” “Japan1HeiMin”

[25] “Japan1GothicBBB” “Japan1Ryumin” “Korea1”

[28] “Korea1deb” “CNS1” “GB1”

plot(1:10,1:10,type="n", col=Color_4for12)

text(3,3,"Hello World Default")

text(4,4,family=Fonts[1], paste0("Hello World, ", Fonts[1]))

text(5,5,family=Fonts[2], paste0("Hello World, ", Fonts[2]))

text(6,6,family=Fonts[3], paste0("Hello World, ", Fonts[3]))

text(7,7,family=Fonts[4], paste0("Hello World, ", Fonts[4]))

Task for lesson

Color the sentences (Hello World) separately. (You can use col=Color_4for12, or create another for example Color_5for12.)

Mac

See here: http://www.r-bloggers.com/changing-the-font-of-r-base-graphic-plots/.

quartzFonts()

Save to svg with letters

library(RSvgDevice)

devSVG(file="Rplots.svg", width=10, height=8, bg="white", fg="black", onefile=TRUE, xmlHeader=TRUE) 

#The width and height are in inch. Save a plot to svg with fonts.

devSVG("try.svg", width=5.55, height=5.55)

plot(1:10,1:10,type="n")

text(3,3,"Hello World Default")

dev.off()
## png 
##   2

Homework

Task 1

Edit the previous homework task 2. Create a boxplot diagram from the counts in log10 scale. Colour with rainbow colour it with the colour scheme used in density plots. Make the x-axes label to horizontal Save the picture as a png file.

Task 2

Create a data.frame with 4 columns and 5 lines. The first column is 1, 2, 3, 4, 5. The remaining columns are always twice the number of the previous column. What is the sum of items in lines 1 and 4? Sort the data.frame by replacing columns 2 and 3. Use a bar graph to show the sum of the elements of the columns.