Creating a boxplot

A boxplot is another important graph that summarizes the data along with the distribution. In this recipe, we will see how we can produce a boxplot in order to visualize the data summary with distributions.

Getting ready

To create the boxplot, we simulated the dataset as per the following code snippet:

# Set a seed value to make the data reproducible
set.seed(12345)
qqdata <-data.frame(disA=rnorm(n=100,mean=20,sd=3),
                disB=rnorm(n=100,mean=25,sd=4),
                disC=rnorm(n=100,mean=15,sd=1.5),
                age=sample((c(1,2,3,4)),size=100,replace=T),
                sex=sample(c("Male","Female"),size=100,replace=T),
                econ_status=sample(c("Poor","Middle","Rich"),
                size=100,replace=T))

How to do it…

The basic command that produces a box plot is as follows:

bwplot(~disA,data=qqdata)
How to do it…

How it works…

The basic command produces the boxplot of a single variable with the horizontal orientation. Now, if we want the box plot in the vertical orientation and want it to repeat for each value of another categorical variable—for example sex—then the syntax will be like the following line of code:

bwplot(disA~sex,data=qqdata)
How it works…

There's more…

To produce multiple boxplots of a numeric variable over the categorical variables, we can simply add the categorical variable to the right-hand side of the formula part:

bwplot(disA~sex|econ_status,data=qqdata)
There's more…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.116