You are on page 1of 10

Thegraphicspackageggplot2ispowerful,aestheticallypleasing,and(afterashort learningcurvetounderstandthesyntax)easytouse.Ihavemadesomeprettycoolplots withit,butonthewholeIfindmyselfmakingalotofthesameones,sincedoingsomething overandoveragainisgenerallyhowresearchgoes.SinceIconstantlyforgettheoptions thatIneedtocustomizemyplots,thisnextseriesofpostswillserveascheatsheetsfor scatterplots,barplots,anddensityplots.Westartwithscatterplots.

QuickIntrotoggplot2
Thewayggplot2worksisbylayeringcomponentsofyourplotontopofeachother.You startwiththebasicofthedatayouwantyourplottoinclude(xandyvariables),andthen layerontopthekindofplottingcolors/symbolsyouwant,thelookofthexandyaxes,the backgroundcolor,etc.Youcanalsoeasilyaddregressionlinesandsummarystatistics. Forgreatreferenceguides,usetheggplot2documentationortheRGraphsCookbook. Inthispost,wefocusonlyonscatterplotswithacontinuousxandcontinuousy.Weare goingtousethemtcarsdatathatisavailablethroughR. l i b r a r y ( g g p l o t 2 ) l i b r a r y ( g r i d E x t r a ) m t c< -m t c a r s Here'sthebasicsyntaxofascatterplot.Wegiveitadataframe,mtc,andthenintheaes() statement,wegiveitanxvariableandayvariabletoplot.Isaveitasaggplotobjectcalled p1,becausewearegoingtousethisasthebaseandthenlayereverythingelseontop: #B a s i cs c a t t e r p l o t p 1< -g g p l o t ( m t c ,a e s ( x=h p ,y=m p g ) ) Nowfortheplottoprint,weneedtospecifythenextlayer,whichishowthesymbolsshould lookdowewantpointsorlines,whatcolor,howbig.Let'sstartwithpoints: #P r i n tp l o tw i t hd e f a u l tp o i n t s p 1+g e o m _ p o i n t ( )

That'sthebarebonesofit.Nowwehavefunwithaddinglayers.Foreachoftheexamples, I'mgoingtousethegrid.arrange()functioninthegridExtrapackagetocreatemultiple graphsinonepaneltosavespace.

>>Changecolorofpoints
Westartwithoptionsforcolorsjustbyaddinghowwewanttocolorourpointsinthe geom_point()layer: p 2< -p 1+g e o m _ p o i n t ( c o l o r = " r e d " ) # s e to n e c o l o rf o ra l lp o i n t s p 3< -p 1+g e o m _ p o i n t ( a e s ( c o l o r=w t ) ) # s e tc o l o r s c a l eb yac o n t i n u o u sv a r i a b l e p 4< -p 1+g e o m _ p o i n t ( a e s ( c o l o r = f a c t o r ( a m ) ) ) # s e tc o l o r s c a l eb yaf a c t o rv a r i a b l e g r i d . a r r a n g e ( p 2 ,p 3 ,p 4 ,n r o w = 1 )

Wecanalsochangethedefaultcolorsthataregivenbyggplot2likethis:

# C h a n g ed e f a u l tc o l o r si nc o l o rs c a l e p 1+g e o m _ p o i n t ( a e s ( c o l o r = f a c t o r ( a m ) ) )+ s c a l e _ c o l o r _ m a n u a l ( v a l u e s=c ( " o r a n g e " ," p u r p l e " ) )

>>Changeshapeorsizeofpoints
We'restickingwiththebasicp1plot,butnowchangingtheshapeandsizeofthepoints: p 2< -p 1+g e o m _ p o i n t ( s i z e=5 ) a l lp o i n t st os i z e5 p 3< -p 1+g e o m _ p o i n t ( a e s ( s i z e=w t ) ) p o i n ts i z eb yc o n t i n u o u sv a r i a b l e p 4< -p 1+g e o m _ p o i n t ( a e s ( s h a p e=f a c t o r ( a m ) ) ) p o i n ts h a p eb yf a c t o rv a r i a b l e g r i d . a r r a n g e ( p 2 ,p 3 ,p 4 ,n r o w = 1 ) # i n c r e a s e # s e t # s e t

Again,ifwewanttochangethedefaultshapeswecan:

p 1+g e o m _ p o i n t ( a e s ( s h a p e=f a c t o r ( a m ) ) )+ s c a l e _ s h a p e _ m a n u a l ( v a l u e s = c ( 0 , 2 ) )

Moreoptionsforcolorandshapemanualchangesarehere Allshapeandlinetypescanbefoundhere:http://www.cookbook r.com/Graphs/Shapes_and_line_types

>>Addlinestoscatterplot
p 2< -p 1+g e o m _ p o i n t ( c o l o r = " b l u e " )+g e o m _ l i n e ( ) # c o n n e c tp o i n t sw i t hl i n e p 3< -p 1+g e o m _ p o i n t ( c o l o r = " r e d " )+g e o m _ s m o o t h ( m e t h o d= " l m " ,s e=T R U E ) # a d dr e g r e s s i o nl i n e p 4< -p 1+g e o m _ p o i n t ( )+g e o m _ v l i n e ( x i n t e r c e p t=1 0 0 , c o l o r = " r e d " ) # a d dv e r t i c a ll i n e g r i d . a r r a n g e ( p 2 ,p 3 ,p 4 ,n r o w = 1 )

Youcanalsotakeoutthepoints,andjustcreatealineplot,andchangesizeandcoloras before:

g g p l o t ( m t c ,a e s ( x=w t ,y=q s e c ) )+g e o m _ l i n e ( s i z e = 2 , a e s ( c o l o r = f a c t o r ( v s ) ) )

Morehelponscatterplotscanbefoundhere:http://www.cookbook r.com/Graphs/Scatterplots_(ggplot2)

>>Changeaxislabels
Thereareafewwaystodothis.Ifyouonlywanttoquicklyaddlabelsyoucanusethe labs()layer.Ifyouwanttochangethefontsizeandstyleofthelabel,thenyouneedtouse thetheme()layer.Moreonthisattheendofthispost.Ifyouwanttochangearoundthe limitsoftheaxis,andexactlywherethebreaksare,youusethescale_x_continuous(and scale_y_continuousfortheyaxis). p 2< -g g p l o t ( m t c ,a e s ( x=h p ,y=m p g ) )+g e o m _ p o i n t ( ) p 3< -p 2+l a b s ( x = " H o r s e p o w e r " , y=" M i l e sp e rG a l l o n " ) # l a b e la l la x e sa to n c e p 4< -p 2+t h e m e ( a x i s . t i t l e . x=e l e m e n t _ t e x t ( f a c e = " b o l d " , s i z e = 2 0 ) )+ l a b s ( x = " H o r s e p o w e r " ) # l a b e la n dc h a n g ef o n ts i z e p 5< -p 2+s c a l e _ x _ c o n t i n u o u s ( " H o r s e p o w e r " , l i m i t s = c ( 0 , 4 0 0 ) , b r e a k s = s e q ( 0 ,4 0 0 ,5 0 ) ) # a d j u s ta x i sl i m i t sa n db r e a k s g r i d . a r r a n g e ( p 3 ,p 4 ,p 5 ,n r o w = 1 )

Moreaxisoptionscanbefoundhere:http://www.cookbook r.com/Graphs/Axes_(ggplot2)

>>Changelegendoptions
Westartoffbycreatinganewggplotbaseobject,g1,whichcolorsthepointsbyafactor variable.Thenweshowthreebasicoptionstomodifythelegend. g 1 < g g p l o t ( m t c ,a e s ( x=h p ,y=m p g ) )+ g e o m _ p o i n t ( a e s ( c o l o r = f a c t o r ( v s ) ) ) g 2< -g 1+ t h e m e ( l e g e n d . p o s i t i o n = c ( 1 , 1 ) , l e g e n d . j u s t i f i c a t i o n = c ( 1 , 1 ) ) # m o v el e g e n di n s i d e g 3< -g 1+t h e m e ( l e g e n d . p o s i t i o n=" b o t t o m " ) # m o v el e g e n db o t t o m g 4< -g 1+s c a l e _ c o l o r _ d i s c r e t e ( n a m e= " E n g i n e " , l a b e l s = c ( " V e n g i n e " , " S t r a i g h te n g i n e " ) ) # c h a n g el a b e l s g r i d . a r r a n g e ( g 2 ,g 3 ,g 4 ,n r o w = 1 )

Ifwehadchangedtheshapeofthepoints,wewouldusescale_shape_discrete()withthe sameoptions.Wecanalsoremovetheentirelegendaltogetherbyusing theme(legend.position=none) Nextwecustomizealegendwhenthescaleiscontinuous: g 5 < g g p l o t ( m t c ,a e s ( x=h p ,y=m p g ) )+g e o m _ p o i n t ( s i z e = 2 , a e s ( c o l o r=w t ) ) g 5+s c a l e _ c o l o r _ c o n t i n u o u s ( n a m e = " W e i g h t " , # n a m eo fl e g e n d b r e a k s=w i t h ( m t c ,c ( m i n ( w t ) , m e a n ( w t ) ,m a x ( w t ) ) ) ,# c h o o s eb r e a k so fv a r i a b l e l a b e l s=c ( " L i g h t " ," M e d i u m " , " H e a v y " ) , # l a b e l l o w=" p i n k " , # c o l o ro fl o w e s tv a l u e h i g h=" r e d " ) # c o l o ro fh i g h e s tv a l u e

Morelegendoptionscanbefoundhere:http://www.cookbook r.com/Graphs/Legends_(ggplot2)

>>Changebackgroundcolorandstyle
Thelookoftheplotintermsofthebackgroundcolorsandstyleisthetheme().Ipersonally don'tlikethelookofthedefaultgraysoherearesomequickwaystochangeit.Ioftenthe theme_bw()layer,whichgetsridofthegray. Allofthethemeoptionscanbefoundhere.

g 2 < -g g p l o t ( m t c ,a e s ( x=h p ,y=m p g ) )+g e o m _ p o i n t ( ) # C o m p l e t e l yc l e a ra l ll i n e se x c e p ta x i sl i n e sa n dm a k e b a c k g r o u n dw h i t e t 1 < t h e m e ( p l o t . b a c k g r o u n d=e l e m e n t _ b l a n k ( ) , p a n e l . g r i d . m a j o r=e l e m e n t _ b l a n k ( ) , p a n e l . g r i d . m i n o r=e l e m e n t _ b l a n k ( ) , p a n e l . b o r d e r=e l e m e n t _ b l a n k ( ) , p a n e l . b a c k g r o u n d=e l e m e n t _ b l a n k ( ) , a x i s . l i n e=e l e m e n t _ l i n e ( s i z e = . 4 ) ) # U s et h e m et oc h a n g ea x i sl a b e ls t y l e t 2 < t h e m e ( a x i s . t i t l e . x=e l e m e n t _ t e x t ( f a c e = " b o l d " ,c o l o r = " b l a c k " , s i z e = 1 0 ) , a x i s . t i t l e . y=e l e m e n t _ t e x t ( f a c e = " b o l d " ,c o l o r = " b l a c k " , s i z e = 1 0 ) , p l o t . t i t l e=e l e m e n t _ t e x t ( f a c e = " b o l d " ,c o l o r=" b l a c k " , s i z e = 1 2 ) ) g 3< -g 2+t 1 g 4< -g 2+t h e m e _ b w ( ) g 5< -g 2+t h e m e _ b w ( )+t 2+l a b s ( x = " H o r s e p o w e r " ,y= " M i l e sp e rG a l l o n " ,t i t l e =" M P Gv sH o r s e p o w e r " ) g r i d . a r r a n g e ( g 2 ,g 3 ,g 4 ,g 5 ,n r o w = 1 )

Finally,here'sanicegraphusingacombinationofoptions:

g 2 < -g g p l o t ( m t c ,a e s ( x=h p ,y=m p g ) )+ g e o m _ p o i n t ( s i z e = 2 ,a e s ( c o l o r = f a c t o r ( v s ) , s h a p e = f a c t o r ( v s ) ) )+ g e o m _ s m o o t h ( a e s ( c o l o r = f a c t o r ( v s ) ) , m e t h o d=" l m " ,s e= T R U E )+ s c a l e _ c o l o r _ m a n u a l ( n a m e= " E n g i n e " , l a b e l s = c ( " V e n g i n e " ," S t r a i g h t e n g i n e " ) , v a l u e s = c ( " r e d " , " b l u e " ) )+ s c a l e _ s h a p e _ m a n u a l ( n a m e= " E n g i n e " , l a b e l s = c ( " V e n g i n e " ," S t r a i g h t e n g i n e " ) , v a l u e s = c ( 0 , 2 ) )+ t h e m e _ b w ( )+ t h e m e ( a x i s . t i t l e . x=e l e m e n t _ t e x t ( f a c e = " b o l d " ,c o l o r = " b l a c k " , s i z e = 1 2 ) , a x i s . t i t l e . y=e l e m e n t _ t e x t ( f a c e = " b o l d " ,c o l o r = " b l a c k " , s i z e = 1 2 ) , p l o t . t i t l e=e l e m e n t _ t e x t ( f a c e = " b o l d " ,c o l o r=" b l a c k " , s i z e = 1 2 ) , l e g e n d . p o s i t i o n = c ( 1 , 1 ) , l e g e n d . j u s t i f i c a t i o n = c ( 1 , 1 ) )+ l a b s ( x = " H o r s e p o w e r " , y=" M i l e sp e rG a l l o n " , t i t l e =" L i n e a rR e g r e s s i o n( 9 5 %C I )o fM P Gv s H o r s e p o w e rb yE n g i n et y p e " ) g 2

You might also like