The plotting idea came to R when the computer displays were very low resolutions, the early 1990s. This time the only way to display scientific resolution graphic was the plotter machine. Which was basically a pen moved by two electronic motors in 2 axes. That’s why we have to work with devices and layers if we do plot in R.
Demonstration of graphics in R
demo("graphics")
Help page for main plot function
?plot
x=c(1:10)
plot(x)
y=c(x^3)
plot(x,y)
plot(x,y, main="Title", xlab="X axis", ylab="Y axis")
Detailed explanation: http://www.statmethods.net/graphs/line.html
type | description |
---|---|
p | points |
l | lines |
o | overplotted points and lines |
b, c | points (empty if “c”) joined by lines |
s, S | stair steps |
h | histogram-like vertical lines |
n | does not produce any points or lines |
plot(x,y, type="p")
plot(x,y, type="l")
plot(x,y, type="o")
plot(x,y, type="b")
Detailed description: http://www.statmethods.net/advgraphs/parameters.html
pch
: Plotting CHaracters image:
plot(x,y, type="b", pch="a")
plot(x,y, type="b", pch="*")
plot(x,y, type="b", pch=0)
plot(x,y, type="b", pch=23)
lty
: Line TYpe
plot(x,y, type="b", pch=23, lty=2)
cex
: Character EXtension
cex
is a number indicating the amount by which plotting text and symbols should be scaled relative to the default. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller, etc.
cex.axis
: magnification of axis annotation relative to cex
cex.lab
: magnification of x and y labels relative to cex
cex.main
: magnification of titles relative to cex
plot(x,y, type="b", pch=23, cex=2, lty=2)
col
: Default plotting color. Some functions (e.g. lines) accept a vector of values that are recycled.
col.axis
: color for axis annotation
col.lab
: color for x and y labels
col.main
: color for titles
fg
: plot foreground color (axes, boxes - also sets col= to same)
bg
: plot background color
plot(x,y, type="b", pch=23, cex=2, col="red", lty=2, fg="gray", bg="yellow")
Create an empty plot and than add points and lines between points (copy the next command to the terminal line-by-line)
plot(NA, xlim=c(1,10), ylim=c(1,1000), xlab="x", ylab="y", main="Plot")
points(x,y, type="p", pch=23, cex=2, col="red", bg="yellow") # Add points
points(x,y, type="c", lty=2, col="blue") # Add line between points
Detailed description: http://www.statmethods.net/graphs/creating.html
You can save the graph in a variety of formats from the menu: File -> Save As.
You can also save the graph via code using one of the following functions.
Function | Output format |
---|---|
pdf("mygraph.pdf") |
pdf file |
win.metafile("mygraph.wmf") |
windows metafile |
png("mygraph.png") |
png file |
jpeg("mygraph.jpg") |
jpeg file |
bmp("mygraph.bmp") |
bmp file |
postscript("mygraph.ps") |
postscript file |
svg("mygraph.svg") |
svg (Scalable Vector Graphics) file |
Closing existing plot with:
dev.off()
## null device
## 1
png("1_plot.png")
plot(NA, xlim=c(1,10), ylim=c(1,1000), xlab="x", ylab="y", main="Plot")
points(x,y, type="p", pch=23, cex=2, col="red", bg="yellow")
points(x,y, type="c", lty=2, col="blue")
dev.off()
## png
## 2
pdf("2_plots.pdf")
plot(x,y, type="b", pch=23, cex=2, lty=2) # First plot to the 1st page
plot(NA, xlim=c(1,10), ylim=c(1,1000), xlab="x", ylab="y", main="Plot") # Second plot to the 2nd page
points(x,y, type="p", pch=23, cex=2, col="red", bg="yellow")
points(x,y, type="c", lty=2, col="blue")
dev.off()
## png
## 2
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, ]
The first 100 genes of all samples
plot(K[1:100,])
All genes of the first 6 samples
plot(K[, 1:6])
Shows the distribution of variables in bins.
hist(K$C15_r1)
Histogram of logarithm of the raw counts.
L=log10(K)
hist(L$C15_r1)
Colored histogram with different number of bins.
hist(L$C15_r1, breaks=20, col="red")
colors: https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/colorPaletteCheatsheet.pdf
Kernel density plots are usually a much more effective way to view the distribution of variables. It an alternative to histograms.
plot(density(L$C15_r1))
Overplot the density of two samples.
Cretae an empty plot and add lines.
plot(NA, xlim=c(1,6), ylim=c(0,1), xlab="Log10 counts", ylab="Density", main="Density plots")
lines(density(L$C15_r1))
lines(density(L$C15_r2))
Overplot the density of all samples.
plot(NA, xlim=c(1,6), ylim=c(0,1), xlab="Log10 counts", ylab="Density", main="Density plots")
for( i in colnames(L)) {
lines(density(L[,i]))
}
Plot the density of all samples and use different colors.
# color vector
Color_4for12=rep(c("blue", "cyan", "orange", "red"), each=3)
Color_4for12
## [1] "blue" "blue" "blue" "cyan" "cyan" "cyan" "orange"
## [8] "orange" "orange" "red" "red" "red"
# plot
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])
}
Add legend to the plot.
Leg=c("C15", "C23", "W15", "W23")
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("blue", "cyan", "orange", "red"), bty="n")
Create a boxplot diagram from the counts in log10 scale. Colour it with the colour scheme used in density plots. Make the x-axes label to horizontal or 45° angle.
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.
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.
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(8,"Set1")
Save their Hexa code to a vector.
Color_1=brewer.pal(8,"Set1")
Plotting the log counts of C15_r1 (x axis) and C15_r2 (y axis).
plot(L[, 1:2])
Creating a vectors of blue colors
display.brewer.pal(9,"PuBu")[3:9]
## NULL
colorRampPalette
interpolate a set of given colors to create new color ramps.
Col_blues=colorRampPalette(brewer.pal(9,"PuBu")[3:9])
densCols
produces a vector containing colors which encode the local densities at each point in a scatterplot.
Col_dens=densCols(L$C15_r1, L$C15_r2, colramp=Col_blues)
plot(L[, 1:2], col=Col_dens)