SlideShare une entreprise Scribd logo
1  sur  76
Télécharger pour lire hors ligne
by
Aureliano Bombarely Gomez
Boyce Thompson Institute for Plant
Research
Tower Road
Ithaca, New York 14853-1801
U.S.A.
Graphs With R
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
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
1. Introduction to R
R is a language and environment for statistical
computing and graphics..
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
1.1 R Software and documentation
https://www.rstudio.com/
RStudio IDE is a powerful and productive user interface for R.
R STUDIO:
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
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()
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
Basic RVariables:
★ Vectors.
★ Matrices and arrays.
★ Dataframes and list.
1.2 Basic R variables
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”)
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).
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
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)
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
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)
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()
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.
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)
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
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)
2. Gallery of R Graphs and Popular Packages
library("maps")
http://rgraphgallery.blogspot.com/
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
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
2.1 Basic Graphs
Data for these examples: http://data.kew.org/cvalues/ Solanaceae
file: data001_genome_size.csv
0 20 40 60 80 100
0500010000150002000025000
Genome Size for Solanaceae
chromosome number
genomesize(Mb)
plot() simple plots
2.1 Basic Graphs
plot(plant_genome_sizes$chromosome_number, plant_genome_sizes$genome_size, pch=19, xlab="chromosome number",
ylab="genome size (Mb)", xlim=c(0,100), main="Genome Size for Solanaceae")
Histogram for Solanaceae Plant Genome Size
genome size
Frequency
0 5000 10000 15000 20000 25000
05101520253035
hist() density plots
2.1 Basic Graphs
hist(plant_genome_sizes$genome_size, breaks=c(100), col=c("black"), xlab="genome size", main="Histogram for
Solanaceae Plant Genome Size")
parviflora
inflata
violacea
altiplana
integrifolia
occidentalis
axillaris
reitzii
saxicola
bonjardinensis
kleinii
scheideana
helianthemoides
variabilis
pubescens
alpicola
bajeensis
interior
littoralis
riograndensis
parodii
guarapuavensis
mantiqueirensis
exserta
hybrida
500 1000 1500 2000
Petunia species genome size
Genome Size (Mb)
dotchart() simple dotplots
2.1 Basic Graphs
petunia_sizes = plant_genome_sizes[plant_genome_sizes$genus == "Petunia",]
dotchart(petunia_sizes$genome_size, labels=petunia_sizes$species, xlab="Genome Size (Mb)", xlim=c(500,2000),
main="Petunia species genome size")
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
Solanaceae genome sizes by Genus
0
2000
4000
6000
8000
10000
barplot() simple bar graphs
2.1 Basic Graphs
genome_size_by_genus = aggregate(plant_genome_sizes$genome_size, by=list(plant_genome_sizes$genus), FUN=mean)
barplot(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")
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
Species Percentage by Genus
pie() simple pie graphs
2.1 Basic Graphs
species_count = table(plant_genome_sizes$genus)
pie(species_count, radius=1, init.angle=90, col=rainbow(12), main="Species Percentage by Genus")
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
0
5000
10000
15000
20000
25000
Genome Size distribution by Genus
boxplot() boxplot graphs
2.1 Basic Graphs
by_genus = split(plant_genome_sizes$genome_size, plant_genome_sizes$genus)
boxplot(by_genus, las=2, main="Genome Size distribution by Genus", col=c("red", "green", "green", "green", "green", "blue",
"blue", "green", "blue", "green", "red", "blue"), cex.axis=0.6)
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
2.2 Gplots and ggplot2
Gplots is an R package with extra tools for graphs
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
0
5000
10000
15000
20000
25000
Genome Size distribution by Genus
n=1 n=18 n=12 n=11 n=4 n=3 n=4 n=9 n=55 n=25 n=39 n=1
2.2 Gplots and ggplot2
Gplots is an R package with extra tools for graphs:
• balloonplot()
• bandplot()
• barplot2() and boxplot2()
• hist2d()
• overplot()
...
boxplot2(by_genus, las=2, main="Genome Size distribution by Genus",
col=c("red", "green", "green", "green", "green", "blue", "blue", "green",
"blue", "green", "red", "blue"), cex.axis=0.6, top=T)
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
2.2 Gplots and ggplot2
Ggplot2 is an R package with grammar tools for graphs
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")
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
2.3 Corrplot
corrplot is an R package for visualization of correlation
matrix
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
2.4 Ape
ape is an R package for analysis of phylogenetic and
evolution
http://www.r-phylo.org/wiki/Main_Page
2.4 Ape
phylog is an R package for tree manipulation
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
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
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
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
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
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
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
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
0
5000
10000
15000
20000
25000
Genome Size distribution by Genus
Only Diploids
Diploids and Tetraploids
From Diploids to Hexaploids
4) Low level plot function
legend()
3. Steps and functions
Atropa
Calibrachoa
Capsicum
Cyphomandra
Datura
Hyoscyamus
Lycium
Lycopersicon
Nicotiana
Petunia
Solanum
Withania
0
5000
10000
15000
20000
25000
Genome Size distribution by Genus
legend(8,23000, legend=c("Only
Diploids", "Diploids and Tetraploids",
"From Diploids to Hexaploids"),
fill=c("green", "blue", "red"))
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
NbC25469554g0001
NbC25730842g0001
NbC26298068g0003
NbS00000194g0006
NbS00000341g0006
NbS00000565g0004
NbS00001525g0108
NbS00001801g0009
NbS00002670g0014
NbS00002765g0028
Gene Expression
0
2
4
6
8
10
12
14
EV10
EV21
TS10
TS21
Example I: Gene expression
File: data002_gene_expression.tab
4. Examples
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
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.
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()
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.
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
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.
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
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.
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
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.
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
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
6) sixth plot
Problem:
Names are incomplete (margin).
Problem:
Names are incomplete (margin).
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, xlab="Gene Name",
las=2)
4. Examples
Solution:
change margins with
par(oma=c(8,0,0,0)
6) sixth plot
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, xlab="Gene Name",
las=2)
4. Examples
7) seventh plot
Problem:
Gene name label is missplaced.
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
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
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
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
9) ninth plot
Problem:
No legend
Problem:
No legend
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)
>legend(40,13,legend=c("EV10", "EV21",
"TS10", "TS21"), fill=c("green", "lightgreen",
"blue", "lightblue"))
4. Examples
Solution:
use legend() function
9) ninth plot
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)
>legend(40,13,legend=c("EV10", "EV21",
"TS10", "TS21"), fill=c("green", "lightgreen",
"blue", "lightblue"))
4. Examples
10) tenth plot
1) ENABLE GRAPHICAL DEVICE
2) GLOBAL GRAPHICAL PARAMS.
3) HIGH-LEVEL PLOTTING COMMAND
4) LOW-LEVEL PLOTTING COMMAND
4. Examples
>data002_gene_expression <= read.delim("~/
Desktop/R_Class_Exercises/
data002_gene_expression.tab")
>gene_exp =
as.matrix(data002_gene_expression[,2:5])
## Default graphical device
>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)
>legend(40,13,legend=c("EV10", "EV21",
"TS10", "TS21"), fill=c("green", "lightgreen",
"blue", "lightblue"))
Example I: Gene expression
Example II:Multiple graphs
File: data001_genome_size.csv
4. Examples
Example II:Multiple graphs
File: data001_genome_size.csv
4. Examples
>par(oma=c(2,2,0,0), mar=c(6,5,6,2))
>layout(matrix(c(1,2,3,3), 2, 2, byrow = TRUE))
>plant_genome_sizes <- read.delim("~/Desktop/R_Class_Exercises/data001_genome_size.csv")
>genome_size_by_genus = aggregate(plant_genome_sizes$genome_size, by=list(plant_genome_sizes$genus),
FUN=mean)
>barplot(genome_size_by_genus$x, names.arg=genome_size_by_genus$Group.1, las=2, cex.names=1,
col="black", main="Solanaceae genome sizes by Genus")
>mtext("Genome Size (Mb)", 2, 4)
species_count = table(plant_genome_sizes$genus)
>pie(species_count, radius=1, init.angle=90, col=rainbow(12), main="Species Percentage by Genus")
>library("gplots")
>by_genus = split(plant_genome_sizes$genome_size, plant_genome_sizes$genus)
>boxplot2(by_genus, las=2, main="Genome Size distribution by Genus", col=c("red", "green", "green", "green",
"green", "blue", "blue", "green", "blue", "green", "red", "blue"), cex.axis=1, top=T)
>legend(8,23000, legend=c("Only Diploids", "Diploids and Tetraploids", "From Diploids to Hexaploids"),
fill=c("green", "blue", "red"))
>mtext("Genome Size (Mb)", 2, 4)
Example II:Multiple graphs
File: data001_genome_size.csv
4. Examples

Contenu connexe

Tendances

Stata Programming Cheat Sheet
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat SheetLaura Hughes
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processingTim Essam
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheetGil Cohen
 
Stata cheat sheet analysis
Stata cheat sheet analysisStata cheat sheet analysis
Stata cheat sheet analysisTim Essam
 
Python for R developers and data scientists
Python for R developers and data scientistsPython for R developers and data scientists
Python for R developers and data scientistsLambda Tree
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching moduleSander Timmer
 
DataFrame in Python Pandas
DataFrame in Python PandasDataFrame in Python Pandas
DataFrame in Python PandasSangita Panchal
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Laura Hughes
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 

Tendances (20)

R language
R languageR language
R language
 
Python crush course
Python crush coursePython crush course
Python crush course
 
Stata Programming Cheat Sheet
Stata Programming Cheat SheetStata Programming Cheat Sheet
Stata Programming Cheat Sheet
 
Stata cheat sheet: data processing
Stata cheat sheet: data processingStata cheat sheet: data processing
Stata cheat sheet: data processing
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
R Language Introduction
R Language IntroductionR Language Introduction
R Language Introduction
 
R for Statistical Computing
R for Statistical ComputingR for Statistical Computing
R for Statistical Computing
 
Stata cheat sheet analysis
Stata cheat sheet analysisStata cheat sheet analysis
Stata cheat sheet analysis
 
R introduction v2
R introduction v2R introduction v2
R introduction v2
 
Python for R developers and data scientists
Python for R developers and data scientistsPython for R developers and data scientists
Python for R developers and data scientists
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
Presentation R basic teaching module
Presentation R basic teaching modulePresentation R basic teaching module
Presentation R basic teaching module
 
Python for R users
Python for R usersPython for R users
Python for R users
 
DataFrame in Python Pandas
DataFrame in Python PandasDataFrame in Python Pandas
DataFrame in Python Pandas
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
Stata Cheat Sheets (all)
Stata Cheat Sheets (all)Stata Cheat Sheets (all)
Stata Cheat Sheets (all)
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Programming in R
Programming in RProgramming in R
Programming in R
 

En vedette (7)

PerlScripting
PerlScriptingPerlScripting
PerlScripting
 
PerlTesting
PerlTestingPerlTesting
PerlTesting
 
BasicLinux
BasicLinuxBasicLinux
BasicLinux
 
GoTermsAnalysisWithR
GoTermsAnalysisWithRGoTermsAnalysisWithR
GoTermsAnalysisWithR
 
Genome Assembly
Genome AssemblyGenome Assembly
Genome Assembly
 
R base graphics
R base graphicsR base graphics
R base graphics
 
RNAseq Analysis
RNAseq AnalysisRNAseq Analysis
RNAseq Analysis
 

Similaire à BasicGraphsWithR

R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine LearningAmanBhalla14
 
R-programming-training-in-mumbai
R-programming-training-in-mumbaiR-programming-training-in-mumbai
R-programming-training-in-mumbaiUnmesh Baile
 
Practical data science_public
Practical data science_publicPractical data science_public
Practical data science_publicLong Nguyen
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Spencer Fox
 
Broom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesBroom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesWork-Bench
 
R Programming - part 1.pdf
R Programming - part 1.pdfR Programming - part 1.pdf
R Programming - part 1.pdfRohanBorgalli
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptxkarthikks82
 
Exploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationExploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationWesley Goi
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in RFlorian Uhlitz
 

Similaire à BasicGraphsWithR (20)

R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
R-programming-training-in-mumbai
R-programming-training-in-mumbaiR-programming-training-in-mumbai
R-programming-training-in-mumbai
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
Q
QQ
Q
 
Practical data science_public
Practical data science_publicPractical data science_public
Practical data science_public
 
Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016Introduction to R Short course Fall 2016
Introduction to R Short course Fall 2016
 
Machine Learning in R
Machine Learning in RMachine Learning in R
Machine Learning in R
 
Perm winter school 2014.01.31
Perm winter school 2014.01.31Perm winter school 2014.01.31
Perm winter school 2014.01.31
 
R Basics
R BasicsR Basics
R Basics
 
Broom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data FramesBroom: Converting Statistical Models to Tidy Data Frames
Broom: Converting Statistical Models to Tidy Data Frames
 
R Programming - part 1.pdf
R Programming - part 1.pdfR Programming - part 1.pdf
R Programming - part 1.pdf
 
Survey Demo
Survey DemoSurvey Demo
Survey Demo
 
Seminar psu 20.10.2013
Seminar psu 20.10.2013Seminar psu 20.10.2013
Seminar psu 20.10.2013
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptx
 
R workshop
R workshopR workshop
R workshop
 
Exploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationExploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience Specialisation
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in R
 
Rtutorial
RtutorialRtutorial
Rtutorial
 
R language introduction
R language introductionR language introduction
R language introduction
 
Big datacourse
Big datacourseBig datacourse
Big datacourse
 

Dernier

Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 

Dernier (20)

Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 

BasicGraphsWithR

  • 1. by Aureliano Bombarely Gomez Boyce Thompson Institute for Plant Research Tower Road Ithaca, New York 14853-1801 U.S.A. Graphs With R
  • 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
  • 10. Basic RVariables: ★ Vectors. ★ Matrices and arrays. ★ Dataframes and list. 1.2 Basic R variables
  • 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
  • 26. 0 20 40 60 80 100 0500010000150002000025000 Genome Size for Solanaceae chromosome number genomesize(Mb) plot() simple plots 2.1 Basic Graphs plot(plant_genome_sizes$chromosome_number, plant_genome_sizes$genome_size, pch=19, xlab="chromosome number", ylab="genome size (Mb)", xlim=c(0,100), main="Genome Size for Solanaceae")
  • 27. Histogram for Solanaceae Plant Genome Size genome size Frequency 0 5000 10000 15000 20000 25000 05101520253035 hist() density plots 2.1 Basic Graphs hist(plant_genome_sizes$genome_size, breaks=c(100), col=c("black"), xlab="genome size", main="Histogram for Solanaceae Plant Genome Size")
  • 28. parviflora inflata violacea altiplana integrifolia occidentalis axillaris reitzii saxicola bonjardinensis kleinii scheideana helianthemoides variabilis pubescens alpicola bajeensis interior littoralis riograndensis parodii guarapuavensis mantiqueirensis exserta hybrida 500 1000 1500 2000 Petunia species genome size Genome Size (Mb) dotchart() simple dotplots 2.1 Basic Graphs petunia_sizes = plant_genome_sizes[plant_genome_sizes$genus == "Petunia",] dotchart(petunia_sizes$genome_size, labels=petunia_sizes$species, xlab="Genome Size (Mb)", xlim=c(500,2000), main="Petunia species genome size")
  • 29. Atropa Calibrachoa Capsicum Cyphomandra Datura Hyoscyamus Lycium Lycopersicon Nicotiana Petunia Solanum Withania Solanaceae genome sizes by Genus 0 2000 4000 6000 8000 10000 barplot() simple bar graphs 2.1 Basic Graphs genome_size_by_genus = aggregate(plant_genome_sizes$genome_size, by=list(plant_genome_sizes$genus), FUN=mean) barplot(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")
  • 30. Atropa Calibrachoa Capsicum Cyphomandra Datura Hyoscyamus Lycium Lycopersicon Nicotiana Petunia Solanum Withania Species Percentage by Genus pie() simple pie graphs 2.1 Basic Graphs species_count = table(plant_genome_sizes$genus) pie(species_count, radius=1, init.angle=90, col=rainbow(12), main="Species Percentage by Genus")
  • 31. Atropa Calibrachoa Capsicum Cyphomandra Datura Hyoscyamus Lycium Lycopersicon Nicotiana Petunia Solanum Withania 0 5000 10000 15000 20000 25000 Genome Size distribution by Genus boxplot() boxplot graphs 2.1 Basic Graphs by_genus = split(plant_genome_sizes$genome_size, plant_genome_sizes$genus) boxplot(by_genus, las=2, main="Genome Size distribution by Genus", col=c("red", "green", "green", "green", "green", "blue", "blue", "green", "blue", "green", "red", "blue"), cex.axis=0.6)
  • 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
  • 34. Atropa Calibrachoa Capsicum Cyphomandra Datura Hyoscyamus Lycium Lycopersicon Nicotiana Petunia Solanum Withania 0 5000 10000 15000 20000 25000 Genome Size distribution by Genus n=1 n=18 n=12 n=11 n=4 n=3 n=4 n=9 n=55 n=25 n=39 n=1 2.2 Gplots and ggplot2 Gplots is an R package with extra tools for graphs: • balloonplot() • bandplot() • barplot2() and boxplot2() • hist2d() • overplot() ... boxplot2(by_genus, las=2, main="Genome Size distribution by Genus", col=c("red", "green", "green", "green", "green", "blue", "blue", "green", "blue", "green", "red", "blue"), cex.axis=0.6, top=T)
  • 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
  • 39. 2.3 Corrplot corrplot is an R package for visualization of correlation matrix
  • 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
  • 42. 2.4 Ape phylog is an R package for tree manipulation
  • 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
  • 50. Atropa Calibrachoa Capsicum Cyphomandra Datura Hyoscyamus Lycium Lycopersicon Nicotiana Petunia Solanum Withania 0 5000 10000 15000 20000 25000 Genome Size distribution by Genus Only Diploids Diploids and Tetraploids From Diploids to Hexaploids 4) Low level plot function legend() 3. Steps and functions Atropa Calibrachoa Capsicum Cyphomandra Datura Hyoscyamus Lycium Lycopersicon Nicotiana Petunia Solanum Withania 0 5000 10000 15000 20000 25000 Genome Size distribution by Genus legend(8,23000, legend=c("Only Diploids", "Diploids and Tetraploids", "From Diploids to Hexaploids"), fill=c("green", "blue", "red"))
  • 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
  • 64. 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 6) sixth plot Problem: Names are incomplete (margin).
  • 65. Problem: Names are incomplete (margin). 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, xlab="Gene Name", las=2) 4. Examples Solution: change margins with par(oma=c(8,0,0,0) 6) sixth plot
  • 66. 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, xlab="Gene Name", las=2) 4. Examples 7) seventh plot Problem: Gene name label is missplaced.
  • 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
  • 70. 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 9) ninth plot Problem: No legend
  • 71. Problem: No legend 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) >legend(40,13,legend=c("EV10", "EV21", "TS10", "TS21"), fill=c("green", "lightgreen", "blue", "lightblue")) 4. Examples Solution: use legend() function 9) ninth plot
  • 72. 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) >legend(40,13,legend=c("EV10", "EV21", "TS10", "TS21"), fill=c("green", "lightgreen", "blue", "lightblue")) 4. Examples 10) tenth plot
  • 73. 1) ENABLE GRAPHICAL DEVICE 2) GLOBAL GRAPHICAL PARAMS. 3) HIGH-LEVEL PLOTTING COMMAND 4) LOW-LEVEL PLOTTING COMMAND 4. Examples >data002_gene_expression <= read.delim("~/ Desktop/R_Class_Exercises/ data002_gene_expression.tab") >gene_exp = as.matrix(data002_gene_expression[,2:5]) ## Default graphical device >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) >legend(40,13,legend=c("EV10", "EV21", "TS10", "TS21"), fill=c("green", "lightgreen", "blue", "lightblue")) Example I: Gene expression
  • 74. Example II:Multiple graphs File: data001_genome_size.csv 4. Examples
  • 75. Example II:Multiple graphs File: data001_genome_size.csv 4. Examples
  • 76. >par(oma=c(2,2,0,0), mar=c(6,5,6,2)) >layout(matrix(c(1,2,3,3), 2, 2, byrow = TRUE)) >plant_genome_sizes <- read.delim("~/Desktop/R_Class_Exercises/data001_genome_size.csv") >genome_size_by_genus = aggregate(plant_genome_sizes$genome_size, by=list(plant_genome_sizes$genus), FUN=mean) >barplot(genome_size_by_genus$x, names.arg=genome_size_by_genus$Group.1, las=2, cex.names=1, col="black", main="Solanaceae genome sizes by Genus") >mtext("Genome Size (Mb)", 2, 4) species_count = table(plant_genome_sizes$genus) >pie(species_count, radius=1, init.angle=90, col=rainbow(12), main="Species Percentage by Genus") >library("gplots") >by_genus = split(plant_genome_sizes$genome_size, plant_genome_sizes$genus) >boxplot2(by_genus, las=2, main="Genome Size distribution by Genus", col=c("red", "green", "green", "green", "green", "blue", "blue", "green", "blue", "green", "red", "blue"), cex.axis=1, top=T) >legend(8,23000, legend=c("Only Diploids", "Diploids and Tetraploids", "From Diploids to Hexaploids"), fill=c("green", "blue", "red")) >mtext("Genome Size (Mb)", 2, 4) Example II:Multiple graphs File: data001_genome_size.csv 4. Examples