No Output
The code first defines a vector Month, which will be used to compare to the FBM vector. Next, we define a numeric vector of length 12 called FBMCounts.
This vector will be used to store the counts for each month from the FBM vector. The for loop is then started with the variable i, ranging from 1 to 12.
Note the parenthesis around the definition of i. A curly bracket,{, starts the code from the loop, which will be ended with a}. Reading from the inside of the loop, when i=1 Month[1] is “Jan”, which is the first element of the Month vector. The which command then checks each element of FBM to see if it is equal to “Jan” and the length command counts them. Note that ==, two equal signs, are a logical test to see if the elements are the same. Technically, the which function creates a vector with the location of “Jan” in FBM and in this case yields (14, 16, 29). The length function simply returns the length of the vector created by which. We then set FBMCounts[1] = 3 or the number of times “Jan” is listed in FBM. The for loop then does this for each value of i from 1 to 12. It was not shown here, but it is good practice to include aprint statement within the for loop to check to see if the loop in working as expected, provided the loop isn’t too long. For example before the last}and on a new line includeprint(paste(”Count for Month”, i, ”is”, FBMCounts[i])). This line will print information to the screen for each value of i with the paste function creating one line of the text, what is in quotes, and the variables.
Give it a try.
We now proceed with almost the same code for the male data. We first define a numeric vector of length 12 to store the results, MBMCounts, and then run a for loop. Since the male data is already integers we don’t need the Month vector. For example, 1 already represents Jan and so the which function simply needs to identify when MBM==1.
R Code
> MBMCounts = vector(mode="numeric", length=12)
> for (i in c(1:12)){MBMCounts[i]=length(which(MBM==i))}
No Output
Boxplot with a Stripchart 55 example here will have two boxplots and a stripchart overlay. As we explain the code we will point out how to adjust some features.
The first two lines generates two data sets of size 250 from a normal dis- tribution. The first has mean 10 and standard deviation 5 and the second 20 and 10, respectively. The third line combines the data into one variable called Data. Theparcommand sets the number of lines for the margins in the order bottom, left, top, and right. This was done because we needed more space on the bottom to align our labels vertically.
The boxplot function creates the boxplot. The first option is the data, which consists of two lists and called Data. You can simply input TenNorm- Five, TwentyNormTen, but the stripchart used next needed the data in the form of one variable and so we also used it for the boxplot. The pch=20 command sets the style of the outliers to solid circles. This is done to make sure they are distinctive from the actual data graphed by the stripchart. The notches in the boxplot are set with notch=TRUE. If you prefer rectangles without the notch either set notch to FALSE or remove the notch options since the default is without the notch. The next three options create thex- axis labels with las=2 placing them perpendicular (0 or deleting las will place them parallel) the at command setting the locations of the labels, and names defining the labels. The boxes are colored blue and red with the col option.
If you prefer horizontal boxplots include horizontal=TRUE. A title is created with the main option and they-axis is labelled with ylab. We did not use xlab within the boxplotfunction because it placed the x-axis caption within the data labels. This is fixed with themtextcommand in the last line of the code, with side=1 placing the text at the bottom and line=6 placing the caption on the sixth line below the axis. Note that with themtextcommand side set to 2, 3, or 4 will place text on the left, top, and right, respectively.
R Code
> TenNormFive=rnorm(250,mean=10,sd=5)
> TwentyNormTen=rnorm(250,mean=20,sd=10)
> Data=list(TenNormFive,TwentyNormTen)
> par(mar=c(8,5,3,3))
> boxplot(Data,pch=20,notch=TRUE,las=2,at=c(1,2), names=c("Norm(10,5)","Norm(20,10)"),col=c("blue",
"red"),main="Boxplot Example",ylab="Value")
> stripchart(Data,vertical=TRUE,method="jitter", jitter=0.1,add=TRUE,pch=1,col=c("gray50","gray50"))
> mtext("Randomly Generated Normal Data",side=1,line=6)
●
●
●
●●
Norm (10,5) Norm (20,10)
0 10 20 30 40
Boxplot Example
Value
Randomly Generated Normal Data
The first option instripchart passes the Data, our two normal data sets, to the command. We set the orientation with vertical=TRUE, which can be removed if you prefer horizontal boxplots above. The next two commands create the plot with the data set spaced out a bit with method = “jitter”
and jitter=0.1. A larger jitter value spaces the data out more. The stripchart has to be added to the boxplot chart with add=TRUE. The last two options pch=1 and col = c(“gray50”,“gray50”), sets the dots to open circles; there are over 25 options, and colors both sets with the same gray tone.
Histogram 57