Hi! I am Mike Marin and in this video we'll discuss making stratified Boxplots
Stratified box plots are useful for examining the relationship between a categorical variable
and a numeric variable within strata or groups formed by a third variable that is categorical.
We will examine the relationship between Smoking and Lung Capacity within age groups or
age strata. We will be working with the LungCapacity data
that was introduced earlier in the series of the videos.
I have already imported this data into R and attached it.
I have also already created an AgeGroup variable. This variable has taken the Age variable and
created a new categorical representation of it
using Age categories of less than 13, 14-15, 16-17, and 18-plus
We can see that the Age of 6 is in the AgeGroup category of less than 13,
the Age of 18 is in the 18-plus category and so on
We can also see that this variable has 4 categories or levels.
To learn more about converting a numeric variable to a categorical variable using the “cat” command
you can watch the following video here.
In the previous video in this series, we discussed making box plots
First, we learned how to make a box plot for
a single variable,
let’s take a look at doing this for Lung Capacity
We can make a boxplot of the LungCap, we can set up y-label of Lung Capacity,
we can also add a title, let’s call this “Boxplot of LungCap”
and I like to set the “las” argument equal to 1 to have the values on the y-axis rotated
We also learned how to create side-by-side boxplots to compare groups.
Let’s take a look at one of these comparing the Lung Capacity for Smokers and Non-Smokers
To do this, we use the ~ (tilda) here, and this will produce a boxplot of LungCapacity
for Smokers and for Non-Smokers separately; Let’s also change this title here to call it
“LungCap vs Smoke”. For more thorough discussion of creating boxplots
you can watch the previous video in this series here
You can see that the Smokers have a larger Lung Capacity on average
which is counter intuitive.
A topic we will explore more in later videos is the idea of confound.
For now, we will briefly mention that the Smoking effect is confounded with the Age effect
Recall, these children are age 3 to 19 years old,
on average, the Smokers are older than the Non-smokers
And, the older children have bigger bodies and therefore they have bigger lung capacities.
and this is what is causing us to observe this counter-intuitive relationship.
Given this, one option we can consider is examining the relationship between Smoking
and Lung Capacity
within Age Strata.
For example, we can look at this relationship for only the 18 year olds or older
As outlined in the earlier video on boxplots
we can use the square brackets to do this
Here, we can take the Lung Capacity and only use those whose Age is greater or equal to
18
and again smoking, only those whose Age is greater or equal to 18
And here we look at Lung Capacity vs. Smoking for the 18-plus year olds.
When we do this, we now observe a more intuitive relationship
within the 18-plus Age Strata, Smokers have a lower Lung Capacity than Non-smokers.
To learn more about the use of the square brackets, you can watch this video here on
subsetting data using square brackets.
We could do this separately for all different Age Strata using the previous command,
but it's a bit nicer to see them all on the same plot
Recall that I have already created a categorical age group variable and introduced this at
the start of the video
As a reminder, let's just take a look at the first five ages
Let's also take a look at the first 5 AgeGroups
and again a reminder of the "levels" for this variable AgeGroup
we can see that there are 4 age categories or 4 age strata that we will work with
Now, we will want to produce boxplots to visualize the relationship between
Lung Capacity and Smoking within each of these age strata
That is, we want boxplots of Lung Capacity for Smokers and for Non-smokers who are in
the 13 or younger AgeGroup,
we also want boxplots of Lung Capacity for Smokers and Non-smokers
in the 14-15 year old AgeGroup
And so on, all appearing on one plot.
Now let's go back to that boxplot command we entered earlier
within this command, we can use the Smoking times the AgeGroup
what this will do is it will take the two Smoking categories (Yes and No)
and cross them with the 4 AgeGroup categories: less than 13, 14-15, 16-17 and 18-plus
to produce 8 separate boxplots
one for each combination of Smoking category and AgeGroup category
Let's also go ahead and edit that title
let's call it "LungCap vs. Smoke by AgeGroup"
We can see the 8 boxplots here.
You can also notice the x-axis labels are overlapping each other, and therefore we don't
see them all
So we can go ahead and rotate the values on the x-axis by setting the las argument equal
to 2 instead of 1;
Now we can see all those names nicely
We can see the first boxplot is for the Non-smokers who are in the 13 or less AgeGroup
The second boxplot are the Smokers in the 13 or less AgeGroup
That third boxplot is the Non-smokers in the 14 to 15 AgeGroup and so on
We can now visualize how Smokers compare with Non-smokers within each of the age strata
by comparing boxplots that are next to each other
and finally we can use colours to help separate the groups visually a bit better
I will use colour number 4 or blue for Non-smokers
and colour number 2 or red for Smokers
To do this all we need to do is adding the colour argument and we use colour 4
and colour 2, and having only specified 2 colours
and 8 boxplots to be produced, R will 'recycle' the colours:
using blue then red, then blue then red and so on
We can see that within age strata Smokers tend to have a lower Lung capacity than Non-Smokers
Quite a different conclusion than when we examined the relationship between Smoking
and Lung Capacity
ignoring age.
Now, we can clean up the x-axis and strata names a little bit if we like
I have prepared a script of code to do this so I will go ahead and just submit these commands
and make this plot look a little bit nicer
and I would suggest that you can watch the following two videos if you'd like to learn
a bit more about modifying plots, re-labeling axes, adding legends and so on.
So the first thing we can do is produce this plot again and we can add a box around it
we can re-label the y-axis
re-label the x-axis
and adding a legend there letting the viewer know that blue indicates Non-smoker
and red indicates Smokers
Thanks for watching this video, make sure to check out our other instructional videos
and subscribe to our channel!