2f making frequency distribution graph rstudio

7
2f: How to make a frequency distribution graph in RStudio I assume you have the data of the shortterm memory study entered in Rstudio (if not, see here ). If you have done this, you find the file Data_STM in the upper left panel. Enter the workspace by opening a new Rscript file (click on the option File) or by clicking on the option Untitled1 in the left upper panel. To make sure the data are well entered, write list(Data_STM), activate the line and click on Run Line(s). This should show you the data of the STMexperiment in the lower left panel. Marc Brysbaert www.palgrave.com/psychology/brysbaert Basic Statistics for Psychologists 2f: Making a frequency distribution graph in RStudio Palgrave, 2011 1

Upload: gordon-freeman

Post on 23-Oct-2015

46 views

Category:

Documents


1 download

DESCRIPTION

2f Making Frequency Distribution Graph RStudio

TRANSCRIPT

Page 1: 2f Making Frequency Distribution Graph RStudio

2f: How to make a frequency distribution graph in RStudio    I assume you have the data of the short‐term memory study entered in Rstudio (if not, see here).  If you have done this, you find the file Data_STM in the upper left panel.  

  

Enter the workspace by opening a new Rscript file (click on the option File) or by clicking on the option Untitled1 in the left upper panel.  To make sure the data are well entered, write list(Data_STM), activate the line and click on Run Line(s).  

  

This should show you the data of the STM‐experiment in the lower left panel.  

Marc Brysbaert  www.palgrave.com/psychology/brysbaert  Basic Statistics for Psychologists                                                                             2f: Making a frequency distribution graph in RStudio  Palgrave, 2011      1 

Page 2: 2f Making Frequency Distribution Graph RStudio

 • Making a frequency distribution graph in R is easy and graphs are one of the strong features of R.  • First, you have to define the spans you are interested in. Suppose we are interested in all spans from 3 to 11. 

This is defined by the command: span = seq(3, 11, by =1) 

• This commands defines the lower values of the intervals as the sequence of values from 3 to 11 with a step function of 1 (i.e., it will go from 3 to 4, to 5, etc). If we define by=2, then the lower values of the spans will go from 3 to 5, to 7, and so on, which means that we will have a grouped frequency distribution table. 

• This is enough to draw a histogram in R. We do this with the following command: hist(Data_STM$STM.span, span, right=FALSE) 

• The following parts are important: Data_STM$STM.span : this is the variable STM.span from the dataset Data_STM. Notice that we must use uppercase if these have been used in the names. Variables from a dataset are defined as dataset$variable (i.e., with a $ sign between the name of the dataset and the name of the variable). 

- Span refers to the lower values of the intervals we defined above right=FALSE makes that the lower value of the next interval are excluded from the present interval. If we had not done this, the interval 3‐4 would exclude the number 3 and include the number 4. Now the interval goes from 3.0 to 3.99999…. 

The Plot is shown in the lower right panel. All that is left to do is embellish it a bit, so that we can use it in a paper. 

Marc Brysbaert  www.palgrave.com/psychology/brysbaert  Basic Statistics for Psychologists                                                                             2f: Making a frequency distribution graph in RStudio  Palgrave, 2011      2 

Page 3: 2f Making Frequency Distribution Graph RStudio

 For instance, we could make the colour of the bars light blue, change the main title and the label of the X‐axis: hist(Data_STM$STM.span, span, right=FALSE, col="lightblue", main="Frequencies of short‐term memory spans", xlab="Short‐term memory span")  

  

We can also change the font size of the main title and the axis values by using the extensions cex.main and cex.lab: hist(Data_STM$STM.span, span, right=FALSE, col="lightblue", main="Frequencies of short‐term memory spans", cex.main=1.5,  xlab="Short‐term memory span", cex.lab=1.3)  

 • Finally we can make the x‐values more informative. The most informative is to use the interval labels. In that way you 

immediately know what each bar stands for. To change the X‐values, we first have to suppress, the values usually given. This is done with the command xaxt='n' in hist. So, we have: 

hist(Data_STM$STM.span, span, right=FALSE, col="lightblue", main="Frequencies of short‐term memory spans", cex.main=1.5, xlab="Short‐term memory span", cex.lab=1.3, xaxt='n') 

Marc Brysbaert  www.palgrave.com/psychology/brysbaert  Basic Statistics for Psychologists                                                                             2f: Making a frequency distribution graph in RStudio  Palgrave, 2011      3 

Page 4: 2f Making Frequency Distribution Graph RStudio

• Next, to be able to write the X‐values in the middle of a column, we need more information about the histogram. We obtain this by not only plotting the histogram, but also saving it in a new variable, which we can call r. We do this as follows: 

r <‐ hist(Data_STM$STM.span, span, right=FALSE, col="lightblue", main="Frequencies of short‐term memory spans", cex.main=1.5, xlab="Short‐term memory span", cex.lab=1.3, xaxt='n')  • All that is left now, is to define the new labels on the X‐axis. For this we use the command: axis(1, at=r$mids, labels=levels(span.cut)) • These are the important elements in the command: 

- 1 : defines the X‐axis (2 would be the Y‐axis) - at : defines where to place the X‐value; r$mids indicates in the middle of bar - levels(span.cut) gives the names of the intervals we defined. 

 If we enter everything in RStudio, we get this output:  

 

Marc Brysbaert  www.palgrave.com/psychology/brysbaert  Basic Statistics for Psychologists                                                                             2f: Making a frequency distribution graph in RStudio  Palgrave, 2011      4 

Page 5: 2f Making Frequency Distribution Graph RStudio

Notice that each bar is now fully labelled. In the present case, this may not be the aesthetically most pleasing option, but it surely is the most informative. Additionally, it extends to grouped frequency distributions as we can see if we define by=2.  

  

An easy way to make the lay‐out nicer is to use the commands: span2 = span[1:length(span)‐1] axis(1, at=r$mids, labels=span2) First, we define span2 as the array span minus the last value (the 11 which is the value beyond the last interval). Then, R automatically defines the X‐values as the middle of the intervals.  For instance, for by=2 this gives:  

 

Marc Brysbaert  www.palgrave.com/psychology/brysbaert  Basic Statistics for Psychologists                                                                             2f: Making a frequency distribution graph in RStudio  Palgrave, 2011      5 

Page 6: 2f Making Frequency Distribution Graph RStudio

For by=1 this gives:  

   

Marc Brysbaert  www.palgrave.com/psychology/brysbaert  Basic Statistics for Psychologists                                                                             2f: Making a frequency distribution graph in RStudio  Palgrave, 2011      6 

Page 7: 2f Making Frequency Distribution Graph RStudio

 We can also add an extra interval, by defining spans up to 12. Then we get: 

  More in general, we can define the range as going from lower than the minimum value observed to larger than the maximum value observed. For instance, you get exactly the same outcome as above when you define span as: span = seq(min(Data_STM$STM.span)‐1, max(Data_STM$STM.span)+2,by=1) This definition allows you to always have your histogram in the right range.   

Marc Brysbaert  www.palgrave.com/psychology/brysbaert  Basic Statistics for Psychologists                                                                             2f: Making a frequency distribution graph in RStudio  Palgrave, 2011      7