57
Outline day 4 – April 17th Basic plo*ng Introduc2on to ggplot2 package Input / Output of images 03-04/2018 140

1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Outlineday4–April17th

•  Basicplo*ng•  Introduc2ontoggplot2package•  Input/Outputofimages

03-04/2018 140

Page 2: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

GraphinginR:basicgraphing

03-04/2018 141

Page 3: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Producinggraphs

•  R-basepackagegraphicsoffersfunc2onsforproducingmanyplots,forexample:–  scaIerplots–plot()–  histograms–hist()–  boxplots–boxplot()–  barplots–barplot()–  piecharts–pie()

03-04/2018 142

basicgraphing

Page 4: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplots

AscaIerplothaspointsthatshowtherela4onshipbetweentwosetsofdata.

03-04/2018 143

basicgraphing

Page 5: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

BasicscaIerplot

•  x<-1:10•  y<-x^2•  plot(x,y)

03-04/2018 144

basicgraphing

Page 6: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplot

•  plot(x,y,col="red",pch=2,type="b",main="apre@ysca@erplot")

03-04/2018 145

basicgraphing

Page 7: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplot

•  plot(x,y,col=1:10,pch=1:10,cex=1:10,type="b",main="apre@ysca@erplot")

03-04/2018 146

basicgraphing

Page 8: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Barplots

Abarchartorbarplotdisplaysrectangularbarswithlengthspropor4onaltothevaluesthattheyrepresent.

03-04/2018 147

basicgraphing

Page 9: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Barplot•  x<-rep(c("PhDstudent","Postdoc","Technician",

"PI"),c(8,10,5,2))•  barplot(table(x))

03-04/2018 148

basicgraphing

Page 10: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Piechart

Apiechartisacircularchartswhichisdividedintoslices,illustra2ngpropor4ons.

03-04/2018 149

basicgraphing

Page 11: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Piechart•  x<-rep(c("PhDstudent","Postdoc","Technician",

"PI"),c(8,10,5,2))

•  pie(table(x))

03-04/2018 150

basicgraphing

Page 12: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Boxplots

Aboxplotisaconvenientwaytodescribethedistribu4onofthedata.

03-04/2018 151

basicgraphing

Page 13: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Boxplots•  x<-matrix(rnorm(1000),ncol=2)•  boxplot(x)

03-04/2018 152

basicgraphing

Page 14: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Histograms

Ahistogramgraphicallysummarizesthedistribu4onofthedata.

03-04/2018 153

basicgraphing

Page 15: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Histogram•  x<-rnorm(200)•  hist(x)

03-04/2018 154

basicgraphing

Page 16: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Expor2nggraphs

•  Graphscanbesavedintomanyformats,including:pdf,jpeg,bmp,4ff.

•  Openthefiletosaveyourplotin: pdf("my_graph.pdf")

•  Produceagraph: plot(1:10)

•  Andcloseit: dev.off()

03-04/2018 155

basicgraphing

Page 17: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Expor2nggraphs•  FromRStudio:

03-04/2018 156

basicgraphing

Page 18: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Exercise8:Basicplots

03-04/2018 157

Page 19: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

GraphinginR:Introduc4ontoggplot2package

03-04/2018 158

Page 20: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ggplot2

•  GraphingpackageinspiredbytheGrammarofGraphicsworkofLelandWilkinson.

•  Atoolthatenablestoconciselydescribethecomponentsofagraphic.

03-04/2018 159

graphinginR:ggplot2

Page 21: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ggplot2

•  Flexible•  Customizable•  PreIy•  Welldocumented

03-04/2018 160

graphinginR:ggplot2

Page 22: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ggplot2

•  Baselayertoplottwovariables:

ggplot(data=dataframe,aes(x=column1,y=column2))

•  Addalayerspecifyingwhatkindofplotyouwant.ExampleofascaIerplot:

ggplot(data=,aes(x=,y=))+geom_point()

03-04/2018 161

graphinginR:ggplot2

Page 23: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplots

•  df1<-data.frame(sample1=rnorm(200),sample2=rnorm(200))

•  ggplot(data=df1,aes(x=sample1,y=sample2))+geom_point()

03-04/2018 162

graphinginR:ggplot2

Page 24: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplots

•  Storethegraphinanobject:p<-ggplot(data=df1,aes(x=sample1,y=sample2))+geom_point()

•  addlayerstothatobject:–  2tlewithgg4tle(label="my2tle")–  ver2callinewithgeom_vline(xintercept=2)–  etc.

•  p+gg2tle(…)+geom_vline(...)

03-04/2018 163

graphinginR:ggplot2

Page 25: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplots

p<-ggplot(data=df1,aes(x=sample1,y=sample2))+geom_point()•  add2tlewithgg4tle

p2<-p+gg4tle(label="2tleoftheplot")

•  drawaver2calwithgeom_vlinep3<-p2+geom_vline(xintercept=0)

03-04/2018 164

graphinginR:ggplot2

Page 26: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplots

•  Coloraccordingtoanothercolumnofthedataframe:

df2<-data.frame(df1,grouping=rep(c("yes","no"),c(80,120)))p<-ggplot(data=df2,aes(x=sample1,y=sample2,color=grouping))+geom_point()

03-04/2018 165

graphinginR:ggplot2

Page 27: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

ScaIerplots

•  p<-ggplot(data=df2,aes(x=sample1,y=sample2,color=grouping))+geom_point()

03-04/2018 166

graphinginR:ggplot2

Page 28: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Boxplotsggplot(data=df2,aes(x=grouping,y=sample1))+geom_boxplot()

03-04/2018 167

graphinginR:ggplot2

Page 29: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Boxplots

Plo*ngbothsample1andsample2?Needtoconvertthedataframeintoalongformat!

03-04/2018 168

graphinginR:ggplot2

Page 30: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Widevslongformats

03-04/2018 169

graphinginR:ggplot2

genename sample1 sample2 grouping

gene1 6 3 UP

gene2 2.7 5.1 DOWN

gene3 3.2 3.3 NONE

gene4 4.7 1.1 UP genename grouping variable value

gene1 UP sample1 6

gene2 DOWN sample1 2.7

gene3 NONE sample1 3.2

gene4 UP sample1 4.7

gene1 UP sample2 3

gene2 DOWN sample2 5.1

gene3 NONE sample2 3.3

gene4 UP sample2 1.1

library(reshape2)?melt

wide

long

Page 31: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Boxplots

Plo*ngbothsample1andsample2?Converttolongformatdf_long<-melt(df2)

03-04/2018 170

graphinginR:ggplot2

Page 32: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Boxplots

ggplot(data=df_long,aes(x=variable,y=value))+geom_boxplot()

03-04/2018 171

graphinginR:ggplot2

Page 33: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Boxplotsggplot(data=df_long,aes(x=variable,y=value,color=grouping))+geom_boxplot()

03-04/2018 172

graphinginR:ggplot2

Page 34: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Barplots

•  p<-ggplot(data=df2,aes(x=grouping))+geom_bar()

03-04/2018 173

graphinginR:ggplot2

Page 35: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Barplots

•  p<-ggplot(data=df2,aes(x=grouping))+geom_bar()

•  Changexaxislabelwithscale_x_discrete:p2<-p+scale_x_discrete(name="countsofyes/no")

•  Swappingxandyaxiswithcoord_flip():p3<-p2+coord_flip()

03-04/2018 174

graphinginR:ggplot2

Page 36: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Barplots

03-04/2018 175

graphinginR:ggplot2

Page 37: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Histograms

•  ggplot(data=df_long,aes(x=value))+geom_histogram()

03-04/2018 176

graphinginR:ggplot2

Page 38: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Histograms

•  ggplot(df_long,aes(x=value,fill=variable))+geom_histogram()

03-04/2018 177

graphinginR:ggplot2

default:stacked!

Page 39: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Histograms

•  ggplot(df_long,aes(x=value,fill=variable))+geom_histogram(posi2on='iden2ty')

03-04/2018 178

graphinginR:ggplot2

Page 40: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Histograms

•  ggplot(df_long,aes(x=value,fill=variable))+geom_histogram(posi2on='dodge')

03-04/2018 179

graphinginR:ggplot2

Page 41: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Exercise9:Introduc4ontoggplot2

03-04/2018 180

Page 42: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Wrapup:whatdidwelearninthose16hours?

•  Rstudio•  Rbasics•  Datastructures–  Vector– Matrix– Dataframe

03-04/2018 181

•  Input/Output•  Packages•  Plo*ng–  basicplo*ng–  ggplot2

Page 43: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

What’snext?

•  Loops•  Func2ons•  RMarkDown•  RShiny...Anythingyouwanttolearn?à [email protected]/[email protected]

03-04/2018 182

Page 44: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

RStudioCheatsheets

hIps://www.rstudio.com/resources/cheatsheets/hIps://www.rstudio.com/wp-content/uploads/2016/11/ggplot2-cheatsheet-2.1.pdf

03-04/2018 183

Page 45: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

OnlinecoursesEDX/Courseracourses:hIps://www.coursera.org/learn/r-programminghIps://www.coursera.org/learn/advanced-rhIps://www.coursera.org/specializa2ons/sta2s2cshIps://www.edx.org/course/introduc2on-r-data-science-microsor-dat204x-4hIps://www.edx.org/course/sta2s2cs-r-harvardx-ph525-1x-0hIps://www.datacamp.com/courses/free-introduc2on-to-r

03-04/2018 184

Page 46: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Forums

Rmailinglists:hIps://www.r-project.org/mail.htmlBioconductorforum:hIps://support.bioconductor.org/

03-04/2018 185

Page 47: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

SarahBonnin–[email protected]–room460

03-04/2018 186

Thanks!

Page 48: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Moreusefulbasiccommands

03-04/2018 187

Page 49: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Somecommonarithme2cfunc2ons

naturallogarithm log(x)exponen2alfunc2on

e^x exp(x)

sine sin(x)

cosine cos(x)

tangent tan(x)

absolutevalue abs(x)

squareroot sqrt(x)

03-04/2018 188

Morecommands

Page 50: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Objectsstoredintheglobalenvironment

•  Lis2ng:ls()orobjects()•  Removingoneobjectfromenvironment:rm(x)•  Removingseveralobjects:rm(x,y)

•  Removingallobjectfromenvironment:rm(list=ls())

03-04/2018 189

Morecommands

Page 51: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Savingobjectsorsession

•  Saveobjectsxandyinto"myobjects.RData"file– save(x,y,file="myobjects.RData")

•  Loadobjectsxandyintocurrentdirectory:–  load("myobjects.RData")

•  Savethecurrentworkspace(allobjects):– save.image(file=".RData")

03-04/2018 190

Morecommands

Page 52: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Commandhistory

•  Last25commands:–  history()

•  Allpreviouscommands–  history(max.show=Inf)

•  Savecommandhistory:–  savehistory()

•  Loadcommandhistory:–  loadhistory()

03-04/2018 191

Morecommands

Page 53: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Informa2onaboutthecurrentsession

sessionInfo()

Rversion

Pla]ormandOSversion

Packagesa@ached

03-04/2018 192

Morecommands

Page 54: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Wri4ngfunc4onsinR

03-04/2018 193

Page 55: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

User-wriIenfunc2ons

•  Func2onsarepiecesofcodewriIentocarryoutspecifiedtasksandbeabletorepeatthemeasily.

•  Rallowsyoutocreateyourownfunc2ons.

Func>ons

03-04/2018 194

Page 56: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Func2ons’structure

myfunc4on<-func4on(arg1,arg2,...){ commands return(something)}

Func>ons

Func4on’sname Crea4ngthefunc4on

myfunc4on’slistofarguments

whatmyfunc4ondoes

Value/objectmyfunc4onreturns

03-04/2018 195

Page 57: 1803 Introduction to R - CRG€¦ · • R-base package graphics offers funcons for producing many plots, for example: – scaer plots – plot() – histograms – hist() – boxplots

Objectsinfunc2onsmyfunc4on<-func4on(arg1){a<-arg1return(a+1)

}

Func>ons

03-04/2018 196

>a<-12>myfunc2on(10)[1]11>a[1]12

>myfunc2on(10)[1]11>aError:object'a'notfound