0% found this document useful (0 votes)
8 views7 pages

Create Frequency Graphs in RStudio

The document describes how to make a frequency distribution graph in RStudio. It explains that you first need to define the spans of interest as a sequence from the minimum to maximum observed value. Then you can use the hist() function to generate a histogram, specifying the data, spans, and additional formatting options like colors and labels. Finally, it demonstrates how to add labels to the x-axis to indicate each bar's interval range for improved clarity and information.

Uploaded by

Gordon Freeman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Create Frequency Graphs in RStudio

The document describes how to make a frequency distribution graph in RStudio. It explains that you first need to define the spans of interest as a sequence from the minimum to maximum observed value. Then you can use the hist() function to generate a histogram, specifying the data, spans, and additional formatting options like colors and labels. Finally, it demonstrates how to add labels to the x-axis to indicate each bar's interval range for improved clarity and information.

Uploaded by

Gordon Freeman
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like