2. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
3. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
4. 1. Introduction to R
R is a language and environment for statistical
computing and graphics..
5. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
6. 1.1 R Software and documentation
https://www.rstudio.com/
RStudio IDE is a powerful and productive user interface for R.
R STUDIO:
7. 1.1 R Software and documentation
1 - Menu
2 - File viewer
3 - Console
4a - Variable list
4b - History
5a - File browser
5b - Plots
5c - Packages
5d - Manuals
8. 1.1 R Software and documentation
WEB:
OFICIAL WEB: http://www.r-project.org/index.html
QUICK-R: http://www.statmethods.net/index.html
BOOKS:
• Introductory Statistics with R (Statistics and Computing), P.
Dalgaard [available as manual at R project web]
• The R Book, MJ. Crawley
R itself: help() and example()
9. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
11. Basic RVariables:
★ Vectors.
1.2 Basic R variables
Most simple 'data structure' in R. Ordered collection
of data values.
To create a vector use command: c()
Examples:
count = c(1, 2, 3, 4, 5, 6)
plants = c(“tomato”, “potato”, “soybean”)
12. Basic RVariables:
★ Vectors.
1.2 Basic R variables
Operation with vectors:
• Addition: x + y
• Substraction: x - y
• Division: x / y
• Exponentation: x ^ y
• Square root: sqrt(x)
• Absolute value: abs(x)
• Mean and median: mean(x) and median(x).
13. Basic RVariables:
★ Matrices and arrays.
1.2 Basic R variables
A matrix is a two dimension vector with positive values.
To create a matrix use command: matrix(c(), ncol, nrow)
Examples:
mtx1 = matrix(c(1, 4, 8, 9, 5, 6, 2, 1, 1), ncol=3, nrow=3)
1 4 8
9 5 6
2 1 1
14. Basic RVariables:
★ Matrices and arrays.
1.2 Basic R variables
Matrices have index and they can be replaced by
names
[ ,1] [ ,2] [ ,3]
[1, ] 1 4 8
[2, ] 9 5 6
[3, ] 2 1 1
mtx1[1,3] 8
mtx1[2,] c(9,5,6)
15. Basic RVariables:
★ Matrices and arrays.
1.2 Basic R variables
An array is a vector with N dimensions
To create an array use command: array(c(), dim=c())
Examples:
array1 = matrix(c(1, 4, 8, 9, 5, 6, 2, 1), dim=c(2,2,2))
1 4
8 9
5 6
2 1
16. Basic RVariables:
★ Matrices and arrays.
1.2 Basic R variables
Arrays also have indexes
array1[1,2,1] 4
array1[2,2] c(9,1)
1 4
8 9
5 6
2 1
array1[2,] matrix(1,4,8,9)
17. Basic RVariables:
★ List and dataframes.
1.2 Basic R variables
A list is an object object consisting of an ordered collection of
other objects as its components
Example:
test = list(c(1, 4, 8, 9, 5, 6, 2, 1), c(“red”, “blue”), mtx1)
To create a list use command: list()
18. Basic RVariables:
★ List and dataframes.
1.2 Basic R variables
A dataframe is a list con 4 specific features:
To create a data frame use command: data.frame()
1.The components must be vectors (numeric, character, or logical), factors,
numeric matrices, lists, or other data frames.
2.Matrices, lists, and data frames provide as many variables to the new data frame
as they have columns, elements, or variables, respectively.
3.Numeric vectors, logicals and factors are included as is, and character vectors are
coerced to be factors, whose levels are the unique values appearing in the vector.
4.Vector structures appearing as variables of the data frame must all have the
same length, and matrix structures must all have the same row size.
19. Basic RVariables:
★ List and dataframes.
1.2 Basic R variables
A dataframe can be imported/exported to/from R using:
read.table(file)
write.table(data.frame, file)
20. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
21. 2. Gallery of R Graphs and Popular Packages
Graphs are R functions that produce a graphical output.
R functions: Objects with a set of instructions to process
some data or to produce a graphical output.
name(arguments)
22. 2. Gallery of R Graphs and Popular Packages
library("maps")
http://rgraphgallery.blogspot.com/
23. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
24. Depends of the package:
+ Basic package functions:
plot() simple plots
hist() density plots
dotchart() simple dotplots
barplot() simple bar graphs
pie() simple pie graphs
boxplot() boxplot graphs
2.1 Basic Graphs
25. 2.1 Basic Graphs
Data for these examples: http://data.kew.org/cvalues/ Solanaceae
file: data001_genome_size.csv
32. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
33. 2.2 Gplots and ggplot2
Gplots is an R package with extra tools for graphs
35. 2.2 Gplots and ggplot2
Gplots is an R package with extra tools for graphs:
• balloonplot()
• bandplot()
• barplot2() and boxplot2()
• hist2d()
• overplot()
...
genome_size_by_genus_sd = aggregate(plant_genome_sizes
$genome_size, by=list(plant_genome_sizes$genus), FUN=sd)
barplot2(genome_size_by_genus$x, names.arg=genome_size_by_genus
$Group.1, las=2, cex.names=0.5, col="black", main="Solanaceae genome
sizes by Genus", ci.l=genome_size_by_genus$x -
genome_size_by_genus_sd$x, ci.u=genome_size_by_genus$x +
genome_size_by_genus_sd$x, plot.ci=T, ylim=c(0,20000))
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
Solanaceae genome sizes by Genus
0
5000
10000
15000
20000
36. 2.2 Gplots and ggplot2
Ggplot2 is an R package with grammar tools for graphs
37. 0e+00
2e−04
4e−04
6e−04
0 5000 10000 15000 20000 25000
genome size
density
poly
Diploid
Tetraploid
Hexaploid
Genome Size density for Solanaceae
2.2 Gplots and ggplot2
Ggplot2 is an R package with grammar tools for graphs
plant_genome_sizes$poly = factor(plant_genome_sizes$polyploidy,
levels=c(2,4,6), labels=c("Diploid", "Tetraploid", "Hexaploid"))
qplot(plant_genome_sizes$genome_size, data=plant_genome_sizes,
geom="density", main="Genome Size density for Solanaceae", fill=poly,
alpha=I(.5), xlab="genome size")
38. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
40. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
41. 2.4 Ape
ape is an R package for analysis of phylogenetic and
evolution
http://www.r-phylo.org/wiki/Main_Page
43. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
44. Graphical procedures step by step:
1) Enable Graphical Device
(X11/QuartzWindows/ by default)
2) Global graphical parameters for the graphical device
3) High level plot function
4) Low level plot function
3. Steps and functions
45. Graphical procedures step by step:
1) Enable Graphical Device
bmp(), tiff(), jpeg(), png(), postscript(), pdf().
2) Global graphical parameters for the graphical device
par()
3) High level plot function
plot(), hist(), dotchart(), barplot(), pie()...
4) Low level plot function
legend(), points(), axis(), text(), mtext()...
3. Steps and functions
46. 2) Global graphical parameters for the graphical device
par()
3. Steps and functions
Text and
Symbol Size
Plotting
Symbols
Lines Colors
Fonts
Margins and
Graph Size
http://www.statmethods.net/advgraphs/parameters.html
47. 2) Global graphical parameters for the graphical device
par()
3. Steps and functions
Margins and Graph Size
http://rgraphics.limnology.wisc.edu/rmargins_sf.php
Margins:
par(mar=c(b,l,t,r))
Outer margins:
par(omar=c(b,l,t,r))
b = bottom
l = left
t = top
r = right
48. 2) Global graphical parameters for the graphical device
par()
3. Steps and functions
Multiple graphs
http://rgraphics.limnology.wisc.edu/rmargins_mfcol.php
Multiple plots:
par(mfcol=(x,y))
x = x_columns
y = y_rows
49. 4) Low level plot function
legend(), points(), axis(), text(), mtext()...
3. Steps and functions
http://www.statmethods.net/advgraphs/axes.html
Titles
Text
Annotations
Axes Legend
Reference
Lines
Points
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
0
5000
10000
15000
20000
25000
Genome Size distribution by Genus
51. Graphs with R:
1. Introduction to R
1.1. R Software and documentation
1.2. Basic R variables
2. Gallery of R Graphs and Popular Packages
2.1. Basic graphs
2.2. Gplots and ggplot2 (expanding graphs)
2.3. Corrplot (correlation matrices)
2.4. Ape (phylogenies)
3. Steps and Functions.
4. Examples
53. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
4. Examples
0) Data load and preparation
data.frame
matrix
54. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(gene_exp)
4. Examples
1) first plot
Problem:
Grouped by conditions, no by gene.
55. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp))
4. Examples
1) first plot
Problem:
Grouped by conditions, no by gene.
Solution:
transpose the matrix with t()
56. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp))
4. Examples
2) second plot
Problem:
Gene expression is stack.
57. Problem:
Gene expression is stack.
Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp), beside=T)
4. Examples
Solution:
use beside=T argument in the
barplot() function
2) second plot
58. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp), beside=T)
4. Examples
3) third plot
Problem:
Wrong limits for the y axis.
59. Problem:
Wrong limits for the y axis.
Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp), beside=T, ylim=c(0,15))
4. Examples
Solution:
use ylim=c(0,15) argument in the
barplot() function
3) third plot
60. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp), beside=T)
4. Examples
4) forth plot
Problem:
No labels for any axis.
61. Problem:
No labels for any axis.
Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp), beside=T, ylim=c(0,15),
ylab="Expression (FPKM)",
names.arg=data002_gene_expression
$gene_short_name, xlab="Gene Name")
4. Examples
Solution:
use ylab=”Expression (FPKM)”,
names.arg=data002_gene_express
ion$gene_short_name and
xlab=”Gene Names”, arguments in
the barplot() function
4) forth plot
62. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp), beside=T, ylim=c(0,15),
ylab="Expression (FPKM)",
names.arg=data002_gene_expression
$gene_short_name, xlab="Gene Name")
4. Examples
5) fifth plot
Problem:
Names in the x axis are too big.
63. Problem:
Names in the x axis are too big.
Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>barplot(t(gene_exp), beside=T, ylim=c(0,15),
ylab="Expression (FPKM)",
names.arg=data002_gene_expression
$gene_short_name, xlab="Gene Name",
las=2)
4. Examples
Solution:
use las=2, argument in the barplot()
function
5) fifth plot
67. Problem:
Gene name label is missplaced.
Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>par(oma=c(8,0,0,0))
>barplot(t(gene_exp), beside=T, ylim=c(0,15),
ylab="Expression (FPKM)",
names.arg=data002_gene_expression
$gene_short_name, las=2)
>mtext("Gene Name", 1, 10)
4. Examples
Solution:
Don’t use xlab= argument. Use
mtext() function instead placing the
label in the outer margin
7) seventh plot
68. Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>par(oma=c(8,0,0,0))
>barplot(t(gene_exp), beside=T, ylim=c(0,15),
ylab="Expression (FPKM)",
names.arg=data002_gene_expression
$gene_short_name, las=2)
>mtext("Gene Name", 1, 10)
4. Examples
8) eighth plot
Problem:
No colors, no title
69. Problem:
No colors, no title
Example I: Gene expression
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
>par(oma=c(8,0,0,0))
>barplot(t(gene_exp), beside=T, ylim=c(0,15),
ylab="Expression (FPKM)",
names.arg=data002_gene_expression
$gene_short_name, las=2, col=c("green",
"lightgreen", "blue", "lightblue"), main="Gene
Expression for TS treatment")
>mtext("Gene Name", 1, 10)
4. Examples
Solution:
use col= and main= arguments to
set up a color and a title
8) eighth plot