2f:HowtomakeafrequencydistributiongraphinRStudio
IassumeyouhavethedataoftheshorttermmemorystudyenteredinRstudio(ifnot,seehere). Ifyouhavedonethis,you findthefileData_STMin theupperleftpanel.
EntertheworkspacebyopeninganewRscriptfile(clickontheoptionFile)orbyclickingontheoptionUntitled1inthe leftupperpanel. Tomakesurethedataare wellentered,write list(Data_STM),activate thelineandclickonRun Line(s).
ThisshouldshowyouthedataoftheSTMexperimentinthelowerleftpanel.
MarcBrysbaert [Link]/psychology/brysbaert BasicStatisticsforPsychologists2f:MakingafrequencydistributiongraphinRStudio 1 Palgrave,2011
MakingafrequencydistributiongraphinRiseasyandgraphsareoneofthestrongfeaturesofR. First,youhavetodefinethespansyouareinterestedin.Supposeweareinterestedinallspansfrom3to11. Thisisdefinedbythecommand: span=seq(3,11,by=1) Thiscommandsdefinesthelowervaluesoftheintervalsasthesequenceofvaluesfrom3to11withastep functionof1(i.e.,itwillgofrom3to4,to5,etc).Ifwedefineby=2,thenthelowervaluesofthespanswillgo from3to5,to7,andsoon,whichmeansthatwewillhaveagroupedfrequencydistributiontable. [Link]: hist(Data_STM$[Link],span,right=FALSE) Thefollowingpartsareimportant: Data_STM$[Link]:thisisthevariableSTM.spanfromthedatasetData_STM.Noticethatwemustuseuppercaseif [Link]$variable(i.e.,witha$signbetween thenameofthedatasetandthenameofthevariable). - Spanreferstothelowervaluesoftheintervalswedefinedabove right=[Link] this,theinterval34wouldexcludethenumber3andincludethenumber4.Nowtheintervalgoesfrom3.0to 3.99999. ThePlotisshown inthelowerright [Link] lefttodois embellishitabit, sothatwecan useitinapaper.
MarcBrysbaert [Link]/psychology/brysbaert BasicStatisticsforPsychologists2f:MakingafrequencydistributiongraphinRStudio 2 Palgrave,2011
Forinstance,wecould makethecolourofthe barslightblue,changethe maintitleandthelabelof theXaxis: hist(Data_STM$[Link], span,right=FALSE, col="lightblue", main="Frequenciesof shorttermmemory spans",xlab="Shortterm memoryspan")
Wecanalsochangethe fontsizeofthemaintitle andtheaxisvaluesby usingtheextensions [Link]: hist(Data_STM$[Link], span,right=FALSE, col="lightblue", main="Frequenciesof shorttermmemory spans",[Link]=1.5, xlab="Shorttermmemory span",[Link]=1.3)
[Link] [Link],wefirsthavetosuppress,thevaluesusuallygiven. Thisisdonewiththecommandxaxt='n'[Link],wehave: hist(Data_STM$[Link],span,right=FALSE,col="lightblue",main="Frequenciesofshorttermmemoryspans", [Link]=1.5,xlab="Shorttermmemoryspan",[Link]=1.3,xaxt='n')
MarcBrysbaert [Link]/psychology/brysbaert BasicStatisticsforPsychologists2f:MakingafrequencydistributiongraphinRStudio 3 Palgrave,2011
Next,tobeabletowritetheXvaluesinthemiddleofacolumn,[Link] obtainthisbynotonlyplottingthehistogram,butalsosavingitinanewvariable,[Link] follows: r<hist(Data_STM$[Link],span,right=FALSE,col="lightblue",main="Frequenciesofshorttermmemoryspans", [Link]=1.5,xlab="Shorttermmemoryspan",[Link]=1.3,xaxt='n') Allthatisleftnow,[Link]: axis(1,at=r$mids,labels=levels([Link])) Thesearetheimportantelementsinthecommand: - 1:definestheXaxis(2wouldbetheYaxis) - at:defineswheretoplacetheXvalue;r$midsindicatesinthemiddleofbar - levels([Link])givesthenamesoftheintervalswedefined. Ifweentereverythingin RStudio,wegetthis output:
MarcBrysbaert [Link]/psychology/brysbaert BasicStatisticsforPsychologists2f:MakingafrequencydistributiongraphinRStudio 4 Palgrave,2011
Noticethateachbaris [Link] presentcase,thismaynot betheaestheticallymost pleasingoption,butit surelyisthemost [Link], itextendstogrouped frequencydistributionsas wecanseeifwedefine by=2.
Aneasywaytomakethe layoutniceristousethe commands: span2= span[1:length(span)1] axis(1,at=r$mids, labels=span2) First,wedefinespan2as thearrayspanminusthe lastvalue(the11whichis thevaluebeyondthelast interval).Then,R automaticallydefinesthe Xvaluesasthemiddleof theintervals. Forinstance,forby=2this gives:
MarcBrysbaert [Link]/psychology/brysbaert BasicStatisticsforPsychologists2f:MakingafrequencydistributiongraphinRStudio 5 Palgrave,2011
Forby=1thisgives:
MarcBrysbaert [Link]/psychology/brysbaert BasicStatisticsforPsychologists2f:MakingafrequencydistributiongraphinRStudio 6 Palgrave,2011
Wecanalsoaddanextrainterval,[Link]:
Moreingeneral,wecandefinetherangeasgoingfromlowerthantheminimumvalueobservedtolargerthanthe [Link],yougetexactlythesameoutcomeasabovewhenyoudefinespanas: span=seq(min(Data_STM$[Link])1,max(Data_STM$[Link])+2,by=1) Thisdefinitionallowsyoutoalwayshaveyourhistogramintherightrange.
MarcBrysbaert [Link]/psychology/brysbaert BasicStatisticsforPsychologists2f:MakingafrequencydistributiongraphinRStudio 7 Palgrave,2011