Exercises: Pltting Cmplex Figures Using R Versin 2017-11
Exercises: Pltting Cmplex Figures in R 2 Licence This manual is 2016-17, Simn Andrews. This manual is distributed under the creative cmmns Attributin-Nn-Cmmercial-Share Alike 2.0 licence. This means that yu are free: t cpy, distribute, display, and perfrm the wrk t make derivative wrks Under the fllwing cnditins: Attributin. Yu must give the riginal authr credit. Nn-Cmmercial. Yu may nt use this wrk fr cmmercial purpses. Share Alike. If yu alter, transfrm, r build upn this wrk, yu may distribute the resulting wrk nly under a licence identical t this ne. Please nte that: Fr any reuse r distributin, yu must make clear t thers the licence terms f this wrk. Any f these cnditins can be waived if yu get permissin frm the cpyright hlder. Nthing in this license impairs r restricts the authr's mral rights. Full details f this licence can be fund at http://creativecmmns.rg/licenses/by-nc-sa/2.0/uk/legalcde
Exercises: Pltting Cmplex Figures in R 3 Exercise 1: Custmising simple plts The file weight_chart.txt cntains data fr a grwth chart fr a typical baby ver the first 9 mnths f its life. Read this file int a data frame using the read.delim functin. Use the plt functin t draw this as a pint and line graph (type= b ) with the fllwing changes. Apply each ne sequentially s yu can see the effect it has n the plt. Change the pint character t be a filled square (pch=15) Change the plt pint size t be 1.5x nrmal size (cex=1.5) Change the line thickness t be twice the default size (lwd=2) Change the y-axis t scale between 2 and 10kg (ylim=c(2,10)) Change the x-axis title t be Age (mnths) (xlab= Age (mnths) ) Change the y-axis title t be Weight (kg) (ylab= Weight (kg) ) Add a suitable title t the tp f the plt (main= Sme title ) The file feature_cunts.txt cntains a summary f the number f features f different types in the muse GRCm38 genme. Lad this int a data frame using read.delim and then plt it as a barplt. Nte that the data yu need t barplt is cntained within the Cunt clumn f the data frame, s yu pass nly that single clumn as the data fr the plt. Once yu have the basic plt make the fllwing changes: The bars shuld be hrizntal rather than vertical (hriz=true). The cunt axis shuld be labelled (xlab= A title ) The feature names shuld be added t the y axis. (set names.arg t the Feature clumn f the data frame) The plt shuld be given a suitable title (main= Sme title ) The text labels shuld all be hrizntal (las=1) Nte that yu can pass this parameter either via par, r as an additinal ptin t barplt. The margins shuld be adjusted t accmmdate the labels (par mar parameter). Yu need t supply a 4 element vectr fr the bttm,left,tp and right margin values. Lk at the value f par()$mar t see what the default values are s yu knw where t start. Nte that yu will have t redraw the barplt after making the changes with par. [Extensin if yu have time] Use this hist functin t plt ut the distributin f 10000 pints sampled frm a standard nrmal distributin (rnrm) alng with anther 10000 pints sampled frm the same distributin but with an ffset f 4. Example: c(rnrm(10000),rnrm(10000)+4) Find a suitable number f breaks t make the plt lk nicer (breaks=10 fr example)
Exercises: Pltting Cmplex Figures in R 4 Exercise 2: Using clur The file male_female_cunts.txt cntains a time series split int male and female cunt values. Plt this as a barplt Make all bars different clurs using the rainbw functin rainbw takes a single argument, which is the number f clurs t generate, eg rainbw(10) Try making the vectr f clurs separately befre passing it as the cl argument t barplt (cl=rainbw(10)). Rather than hard cding the number f clurs, think hw yu culd use nrw t autmatically generate the crrect number f clurs fr the size f dataset. Replt, and make the bars fr the males a different clur t thse fr the females. In this case the male and female samples alternate s yu can just pass a 2 clur vectr t the cl parameter t achieve this effect. (cl=c( blue2, red2 )) fr example. The file up_dwn_expressin.txt cntains an expressin cmparisn dataset, but has an extra clumn which classifies the rws int ne f 3 grups (up,dwn r unchanging). Plt this as a scatterplt (plt) with the up being red, the dwn being blue and the unchanging being grey. Read in the file using read.delim Start by just pltting the Cnditin1 clumn against the Cnditin2 clumn in a plt Pass the State clumn as the cl parameter (cl=up.dwn$state fr example). This will set the clur accrding t the state f each pint, but the clurs will be set autmatically frm the utput f palette Run palette() t see what clurs are there initially and check that yu can see hw these relate t the clurs yu get in yur plt. Run levels() n the State clumn data and match this with what yu saw in palette() t see hw each clur was selected. Wrk ut what clurs yu wuld need t put int palette t get the clur selectin yu actually want. Use the palette functin t set the crrespnding clurs yu want t use (eg palette(c( red, green, blue )) but using the crrect clurs in the crrect rder. Redraw the plt and check that the clurs are nw what yu wanted. The file clur_t_value_map.r cntains a functin t map a value frm a range t a clur frm a predefined palette. The file expressin_methylatin.txt cntains data fr gene bdy methylatin, prmter methylatin and gene expressin. Draw a scatterplt (plt) f the prmter.meth clumn against the gene.meth clumn. Nw yu can wrk n cluring this plt by the expressin clumn. Fr this yu are ging t need t run the map.clurs functin we prvided. This requires tw bits f
Exercises: Pltting Cmplex Figures in R 5 data ne is simply the values in the expressin clumn, but the ther is a vectr f clurs which define yur clur scale. Yu will need t start by cnstructing a clur palette functin frm grey t red. Run clrramppalette(c( grey, red )) t shw that yu can generate a functin which will make clurs running frm grey t red. Call the functin t generate a set f 100 clurs and save these int a suitably named variable ie: clrramppalette(c( grey, red ))(100). Nw generate yur actual clur vectr yu re ging t use. Call the map.clurs functin yu ve been given with the set f expressin clumn values, and the vectr f clurs yu just generated. Save the per pint clurs which are returned int a new variable. Finally redraw the plt passing the per pint clurs t the cl parameter.
Exercises: Pltting Cmplex Figures in R 6 Exercise 3: Using Overlays The file chrmsme_psitin_data.txt cntains psitinal cunt data fr 3 different datasets (a WT and tw mutants). Plt this as a line graph (using plt(type= l )) shwing the 3 different datasets verlaid. Yu ll need t d an initial plt with type= l specified fllwed by tw additinal layers using the lines functin. The x values will be the Psitin clumn in each case. The y values will be the WT clumn fr the initial plt, and then the Mut1 and Mut2 clumns fr the verlays. Remember t calculate the full range f values acrss all 3 datasets when ding the initial plt s that all f the data will fit int the plt area. Yu can pass the whle data prtin f the data frame (excluding the psitins) t range t d the calculatin ie: range(chr_psitins[,2:4]) Fr the clurs generate a 3 clur palette frm the Set1 RClrBrewer set using the brewer.pal(3, Set1 ) functin and save it. Pass a different clur frm the palette t each f the pltting functins yu call. Use the legend functin, t put a legend at the tpleft f the plt with the data names and the crrespnding clurs (using the fill parameter). Make the lines 2x as thick as standard (lwd=2) Add suitable labels t each axis (using xlab and ylab). The general structure fr yur cde will therefre be: plt( [psitin data], [wt data], ylim=[range data], type= l, cl=[first clur], lwd=2 ) lines([psitin data], [mut1 data], cl=[secnd clur], lwd=2) lines([psitin data], [mut2 data], cl=[third clur], lwd=2) legend( tpleft,c( wt, mut1, mut2 ),fill=[vectr f 3 clurs])
Exercises: Pltting Cmplex Figures in R 7 The file brain_bdyweight.txt cntains data fr the lg10 brain and bdyweight fr a range f species, alng with an SEM measure fr each pint. Plt these data n a scatterplt (using plt) with errr bars shwing the mean +/- SEM and the names f the datasets under each pint. Yu will initially need t d a plt passing the Bdyweight and Brainweight clumns as the data. Make sure this wrks befre trying t add errr bars. Yu will need t add 2 sets f errr bars, ne fr the Bdyweight and ne fr the Brainweight. These will be tw separate additinal layers, each cnsisting f an arrws functin. The arguments fr each arrws functin are: The x (brainweight) start psitin The y (bdyweight) start psitin The x (brainweight) end psitin The y (bdyweight) end psitin Additinal cmmn ptins will be angle=90 t prduce a flat head t the bars cde=3 t get a bar at bth ends f the line length=0.05 s the bars aren t t lng. The start and end psitins fr ne errr bar will be the riginal value plus and minus the crrespnding SEM. Fr example fr the brainweight cnfidence interval the cde wuld be smething like: arrws ( data$bdyweight, data$brainweight data$brainweight.sem, data$bdyweight, data$brainweight + data$brainweight.sem, angle=90, cde=3, length=0.05 ) Yu will, f curse, need t replace data with whatever yu actually called the data frame cntaining the data fr this exercise. Yu wuld als need t repeat this mdifying the Bdyweight values t get the Bdyweight errr bars. T add the names f the species just belw each data pint yu wuld use the text functin. The x and y values will just be the same as fr the riginal plt (Brainweight and Bdyweight). The labels will be the Species clumn. Other ptins yu will need t set will be: ps=1 t put the text underneath each pint cex=0.7 t make the text slightly smaller
Exercises: Pltting Cmplex Figures in R 8 Plt Results Belw are examples f what the plts yu will draw in these exercises shuld end up lking like. Exercise 1:
Exercise 2: Exercises: Pltting Cmplex Figures in R 9
Exercises: Pltting Cmplex Figures in R 10
Exercise 3: Exercises: Pltting Cmplex Figures in R 11