graphing in r · scaer plots a sca&er plot is a type of plot or mathema2cal diagram using...

58
Outline day 4 – May 30th Graphing in R: basic graphing ggplot2 package 05/2017 117

Upload: others

Post on 18-Jan-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Outlineday4–May30th

•  GraphinginR:– basicgraphing– ggplot2package

05/2017 117

Page 2: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

GraphinginR:basicgraphing

05/2017 118

Page 3: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Producinggraphs

•  R-basepackagegraphicsoffersfuncAonsforproducingmanyplots,forexample:–  scaFerplots–plot()–  histograma–hist()–  boxplots–boxplot()–  barplots–barplot()–  piecharts–pie()

05/2017 119

basicgraphing

Page 4: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplots

Asca&erplotisatypeofplotormathema2caldiagramusingCartesiancoordinatestodisplayvaluesfortypicallytwovariablesforasetofdata.[Wikipedia]

05/2017 120

basicgraphing

Page 5: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

BasicscaFerplot

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

05/2017 121

basicgraphing

Page 6: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplot

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

05/2017 122

basicgraphing

Page 7: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplot

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

05/2017 123

basicgraphing

Page 8: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Barplots

Abarchartorbargraphisachartthatpresentsgroupeddatawithrectangularbarswithlengthspropor2onaltothevaluesthattheyrepresent.[Wikipedia]

05/2017 124

basicgraphing

Page 9: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Barplot•  x<-rep(c("geek","nerd","dork"),c(10,5,3))

•  barplot(table(x))

05/2017 125

basicgraphing

Page 10: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Piechart

Apiechartisacircularsta2s2calgraphicwhichisdividedintoslicestoillustratenumericalpropor2on.[Wikipedia]

05/2017 126

basicgraphing

Page 11: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Piechart•  x<-rep(c("geek","nerd","dork"),c(10,5,3))

•  pie(table(x))

05/2017 127

basicgraphing

Page 12: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Boxplots

Indescrip2vesta2s2cs,aboxplotisaconvenientwayofgraphicallydepic2nggroupsofnumericaldatathroughtheirquar2les.

[Wikipedia]

05/2017 128

basicgraphing

Page 13: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

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

05/2017 129

basicgraphing

Page 14: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Histograms

Ahistogramisaplotthatletsyoudiscover,andshow,theunderlyingfrequencydistribu2onofasetofcon2nuousdata.

[Wikipedia]

05/2017 130

basicgraphing

Page 15: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

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

05/2017 131

basicgraphing

Page 16: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ExporAnggraphs

•  Graphscanbesavedintomanyformats,including:pdf,jpeg,bmp,Dff.

•  Openthefiletosaveyourplotin: pdf("my_graph.pdf")

•  Produceagraph: plot(1:10)

•  Andcloseit: dev.off()

05/2017 132

basicgraphing

Page 17: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ExporAnggraphs•  FromRStudio:

05/2017 133

basicgraphing

Page 18: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Exercise9:Basicplots

05/2017 134

Page 19: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

GraphinginR:IntroducDontoggplot2package

05/2017 135

Page 20: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ggplot2

•  GraphingpackageinspiredbytheGrammarofGraphicsseminalworkofLelandWilkinson.

•  Atoolthatenablestoconciselydescribethecomponentsofagraphic.

05/2017 136

graphinginR:ggplot2

Page 21: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ggplot2

•  Flexible•  Customizable•  PreFy•  Welldocumented

05/2017 137

graphinginR:ggplot2

Page 22: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ggplot2

•  Baselayertoplottwovariables:

ggplot(dataframe,aes(x,y))

•  Addlayerspecifyingwhatkindofplotyouwant.ExampleofascaFerplot:

ggplot(dataframe,aes(x,y))+geom_point()

05/2017 138

graphinginR:ggplot2

Page 23: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplots

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

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

05/2017 139

graphinginR:ggplot2

Page 24: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplots

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

•  addlayerstop:–  AtlewithggDtle("myAtle")–  verAcallinewithgeom_vline(xintercept=2)–  etc.

•  p+ggAtle(…)

05/2017 140

graphinginR:ggplot2

Page 25: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplots

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

p2<-p+ggDtle("Atleoftheplot")

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

05/2017 141

graphinginR:ggplot2

Page 26: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplots

•  Coloraccordingtoanothercolumn:

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

05/2017 142

graphinginR:ggplot2

Page 27: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ScaFerplots

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

05/2017 143

graphinginR:ggplot2

Page 28: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Boxplots

•  ggplot(df2,aes(x=1,y=sample1))+geom_boxplot()

05/2017 144

graphinginR:ggplot2

Page 29: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

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

05/2017 145

graphinginR:ggplot2

Page 30: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Boxplots

Plofngbothsample1andsample2?Needtoconvertthedataframeintoalongformat!

05/2017 146

graphinginR:ggplot2

Page 31: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Widevslongformats

05/2017 147

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 32: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Boxplots

Plofngbothsample1andsample2?Converttolongformatdf_long<-melt(df2)

05/2017 148

graphinginR:ggplot2

Page 33: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Boxplots

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

05/2017 149

graphinginR:ggplot2

Page 34: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Boxplots

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

05/2017 150

graphinginR:ggplot2

Page 35: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Barplots

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

05/2017 151

graphinginR:ggplot2

Page 36: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Barplots

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

•  Changexaxislabelwithscale_x_discrete:p2<-p+scale_x_discrete(name=”Howmanyyesandno")

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

05/2017 152

graphinginR:ggplot2

Page 37: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Barplots

05/2017 153

graphinginR:ggplot2

Page 38: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Histograms

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

05/2017 154

graphinginR:ggplot2

Page 39: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Histograms

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

05/2017 155

graphinginR:ggplot2

default:stacked!

Page 40: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Histograms

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

05/2017 156

graphinginR:ggplot2

Page 41: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Histograms

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

05/2017 157

graphinginR:ggplot2

Page 42: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Exercise10:IntroducDontoggplot2

05/2017 158

Page 43: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Wrapup:whatdidwelearninthose12hours?

•  RStudio•  Datastructures– Vector– Matrix– Dataframe– Lists

05/2017 159

•  Input/Output•  Packages•  Plofng– basicplofng– ggplot2

Page 44: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

What’snext?

•  Statsnextweek!

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

05/2017 160

Page 45: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

RStudioCheatsheets

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

05/2017 161

Page 46: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Onlinecourses

EDX/Courseracourses:hFps://www.coursera.org/learn/r-programminghFps://www.coursera.org/learn/advanced-rhFps://www.coursera.org/specializaAons/staAsAcshFps://www.edx.org/course/introducAon-r-data-science-microsos-dat204x-4hFps://www.edx.org/course/staAsAcs-r-harvardx-ph525-1x-0

05/2017 162

Page 47: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Forums

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

05/2017 163

Page 48: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

SarahBonnin–[email protected][email protected]árcel

05/2017 164

Thanks!

Page 49: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Moreusefulbasiccommands

05/2017 165

Page 50: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

SomecommonarithmeAcfuncAons

naturallogarithm log(x)exponenAalfuncAon

e^x exp(x)

sine sin(x)

cosine cos(x)

tangent tan(x)

absolutevalue abs(x)

squareroot sqrt(x)

05/2017 166

Morecommands

Page 51: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Objectsstoredintheglobalenvironment

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

•  Removingallobjectfromenvironment:rm(list=ls())

05/2017 167

Morecommands

Page 52: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Savingobjectsorsession

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

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

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

05/2017 168

Morecommands

Page 53: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

Commandhistory

•  Last25commands:–  history()

•  Allpreviouscommands–  history(max.show=Inf)

•  Savecommandhistory:–  savehistory()

•  Loadcommandhistory:–  loadhistory()

05/2017 169

Morecommands

Page 54: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

InformaAonaboutthecurrentsession

sessionInfo()

Rversion

Pla\ormandOSversion

Packagesa?ached

05/2017 170

Morecommands

Page 55: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

WriDngfuncDonsinR

05/2017 171

Page 56: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

User-wriFenfuncAons

•  FuncAonsarepiecesofcodewriFentocarryoutspecifiedtasksandbeabletorepeatthemeasily.

•  RallowsyoutocreateyourownfuncAons.

Func=ons

05/2017 172

Page 57: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

FuncAons’structure

myfuncDon<-funcDon(arg1,arg2,...){ commands return(something)}

Func=ons

FuncDon’sname CreaDngthefuncDon

myfuncDon’slistofarguments

whatmyfuncDondoes

Value/objectmyfuncDonreturns

05/2017 173

Page 58: Graphing in R · Scaer plots A sca&er plot is a type of plot or mathema2cal diagram using Cartesian coordinates to display values for typically two variables for a set of data

ObjectsinfuncAonsmyfuncDon<-funcDon(arg1){a<-arg1return(a+1)

}

Func=ons

05/2017 174

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

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