Lab 1D
Directions: Follow along with the slides and answer the questions in red font in your journal.
food
.dotPlot
is another plot that can be used to analyze a numerical variable.
dotPlot()
function to create a dotPlot
of the amount of sugar
in our food
data.
dotPlot
is exactly like you’d use to make a histogram
.dotPlot
.While a dotPlot
should conserve the exact value of each data point, sometimes it behaves like a histogram in that it lumps values together.
Create a more accurate dotPlot
by including the nint
option.
food
data spreadsheet, click on the sugar header to sort in ascending order (to obtain minimum)nint
with the histogram
function.Pro-tip: If the dotPlot
comes out looking wonky, try changing the value of the character expansion option, cex
.
1
. Try a few values between 0
and 1
and a few more values larger than 1
.In lab 1B, we learned that we can facet (or split) our data based on a categorical variable.
Split the dotPlot
displaying the distribution of grams of sugar in two, by faceting on our observations’ salty_sweet
variable.
layout
option in our dotPlot
function.
dotPlot
split by salty_sweet
nint
option to add the layout
option to the dotPlot
function.Salty
snacks from our food
data:food_salty
and write down the number of observations in it. Then use the subset data to make a dotPlot of the sodium
in our Salty
snacks.filter()
tells R that we’re going to look at only the values in our data that follow a rule.salty_sweet == "Salty"
is the rule to follow.salty_sweet == "Salty"
, into 3 parts:
salty_sweet
, is the particular variable we want to use to select our subset."Salty"
, is the value of the variable that we want to select. We only want to see data with the value Salty
for the variable salty_sweet
.==
describes how we want to relate our variable (salty_sweet
) to our value ("Salty"
). In this case, we want values of salty_sweet
that are exactly equal to "Salty"
.head()
function to help us see what’s happening when we write salty_sweet == "Salty"
.
head()
returns the values of the first 6 observations.tail()
function returns the last 6 observations.TRUE
and FALSE
tell us about how our rule applies to the first six snacks in our data? Which of the first six observations were Salty
?food_salty
.food_salty
to do anything we could do with the regular food
data …
Salty
.food
data based on the food being salty AND having less than 200 calories.View
the my_sub
data we filtered in the above line of code and verify that it only includes salty snacks that have less than 200 calories.Use an appropriate dotPlot
to answer each of the following questions:
sugar
does the typical sweet snack have?sugar
compare when healthy_level < 3
and when healthy_level > 3
?Because you are now working with subsets of data, it is important to be able to label our plots and make this distinction.
main
option to add a title to our plots
dotPlot
of the sugar in Sweet
snacks.