Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
Outlineday4–April17th
• Basicplo*ng• Introduc2ontoggplot2package• Input/Outputofimages
03-04/2018 140
GraphinginR:basicgraphing
03-04/2018 141
Producinggraphs
• R-basepackagegraphicsoffersfunc2onsforproducingmanyplots,forexample:– scaIerplots–plot()– histograms–hist()– boxplots–boxplot()– barplots–barplot()– piecharts–pie()
03-04/2018 142
basicgraphing
ScaIerplots
AscaIerplothaspointsthatshowtherela4onshipbetweentwosetsofdata.
03-04/2018 143
basicgraphing
BasicscaIerplot
• x<-1:10• y<-x^2• plot(x,y)
03-04/2018 144
basicgraphing
ScaIerplot
• plot(x,y,col="red",pch=2,type="b",main="apre@ysca@erplot")
03-04/2018 145
basicgraphing
ScaIerplot
• plot(x,y,col=1:10,pch=1:10,cex=1:10,type="b",main="apre@ysca@erplot")
03-04/2018 146
basicgraphing
Barplots
Abarchartorbarplotdisplaysrectangularbarswithlengthspropor4onaltothevaluesthattheyrepresent.
03-04/2018 147
basicgraphing
Barplot• x<-rep(c("PhDstudent","Postdoc","Technician",
"PI"),c(8,10,5,2))• barplot(table(x))
03-04/2018 148
basicgraphing
Piechart
Apiechartisacircularchartswhichisdividedintoslices,illustra2ngpropor4ons.
03-04/2018 149
basicgraphing
Piechart• x<-rep(c("PhDstudent","Postdoc","Technician",
"PI"),c(8,10,5,2))
• pie(table(x))
03-04/2018 150
basicgraphing
Boxplots
Aboxplotisaconvenientwaytodescribethedistribu4onofthedata.
03-04/2018 151
basicgraphing
Boxplots• x<-matrix(rnorm(1000),ncol=2)• boxplot(x)
03-04/2018 152
basicgraphing
Histograms
Ahistogramgraphicallysummarizesthedistribu4onofthedata.
03-04/2018 153
basicgraphing
Histogram• x<-rnorm(200)• hist(x)
03-04/2018 154
basicgraphing
Expor2nggraphs
• Graphscanbesavedintomanyformats,including:pdf,jpeg,bmp,4ff.
• Openthefiletosaveyourplotin: pdf("my_graph.pdf")
• Produceagraph: plot(1:10)
• Andcloseit: dev.off()
03-04/2018 155
basicgraphing
Expor2nggraphs• FromRStudio:
03-04/2018 156
basicgraphing
Exercise8:Basicplots
03-04/2018 157
GraphinginR:Introduc4ontoggplot2package
03-04/2018 158
ggplot2
• GraphingpackageinspiredbytheGrammarofGraphicsworkofLelandWilkinson.
• Atoolthatenablestoconciselydescribethecomponentsofagraphic.
03-04/2018 159
graphinginR:ggplot2
ggplot2
• Flexible• Customizable• PreIy• Welldocumented
03-04/2018 160
graphinginR:ggplot2
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
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
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
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
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
ScaIerplots
• p<-ggplot(data=df2,aes(x=sample1,y=sample2,color=grouping))+geom_point()
03-04/2018 166
graphinginR:ggplot2
Boxplotsggplot(data=df2,aes(x=grouping,y=sample1))+geom_boxplot()
03-04/2018 167
graphinginR:ggplot2
Boxplots
Plo*ngbothsample1andsample2?Needtoconvertthedataframeintoalongformat!
03-04/2018 168
graphinginR:ggplot2
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
Boxplots
Plo*ngbothsample1andsample2?Converttolongformatdf_long<-melt(df2)
03-04/2018 170
graphinginR:ggplot2
Boxplots
ggplot(data=df_long,aes(x=variable,y=value))+geom_boxplot()
03-04/2018 171
graphinginR:ggplot2
Boxplotsggplot(data=df_long,aes(x=variable,y=value,color=grouping))+geom_boxplot()
03-04/2018 172
graphinginR:ggplot2
Barplots
• p<-ggplot(data=df2,aes(x=grouping))+geom_bar()
03-04/2018 173
graphinginR:ggplot2
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
Barplots
03-04/2018 175
graphinginR:ggplot2
Histograms
• ggplot(data=df_long,aes(x=value))+geom_histogram()
03-04/2018 176
graphinginR:ggplot2
Histograms
• ggplot(df_long,aes(x=value,fill=variable))+geom_histogram()
03-04/2018 177
graphinginR:ggplot2
default:stacked!
Histograms
• ggplot(df_long,aes(x=value,fill=variable))+geom_histogram(posi2on='iden2ty')
03-04/2018 178
graphinginR:ggplot2
Histograms
• ggplot(df_long,aes(x=value,fill=variable))+geom_histogram(posi2on='dodge')
03-04/2018 179
graphinginR:ggplot2
Exercise9:Introduc4ontoggplot2
03-04/2018 180
Wrapup:whatdidwelearninthose16hours?
• Rstudio• Rbasics• Datastructures– Vector– Matrix– Dataframe
03-04/2018 181
• Input/Output• Packages• Plo*ng– basicplo*ng– ggplot2
What’snext?
• Loops• Func2ons• RMarkDown• RShiny...Anythingyouwanttolearn?à [email protected]/[email protected]
03-04/2018 182
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
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
Forums
Rmailinglists:hIps://www.r-project.org/mail.htmlBioconductorforum:hIps://support.bioconductor.org/
03-04/2018 185
Moreusefulbasiccommands
03-04/2018 187
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
Objectsstoredintheglobalenvironment
• Lis2ng:ls()orobjects()• Removingoneobjectfromenvironment:rm(x)• Removingseveralobjects:rm(x,y)
• Removingallobjectfromenvironment:rm(list=ls())
03-04/2018 189
Morecommands
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
Commandhistory
• Last25commands:– history()
• Allpreviouscommands– history(max.show=Inf)
• Savecommandhistory:– savehistory()
• Loadcommandhistory:– loadhistory()
03-04/2018 191
Morecommands
Informa2onaboutthecurrentsession
sessionInfo()
Rversion
Pla]ormandOSversion
Packagesa@ached
03-04/2018 192
Morecommands
Wri4ngfunc4onsinR
03-04/2018 193
User-wriIenfunc2ons
• Func2onsarepiecesofcodewriIentocarryoutspecifiedtasksandbeabletorepeatthemeasily.
• Rallowsyoutocreateyourownfunc2ons.
Func>ons
03-04/2018 194
Func2ons’structure
myfunc4on<-func4on(arg1,arg2,...){ commands return(something)}
Func>ons
Func4on’sname Crea4ngthefunc4on
myfunc4on’slistofarguments
whatmyfunc4ondoes
Value/objectmyfunc4onreturns
03-04/2018 195
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