Lab 1D
Directions: Follow along with the slides, completing
the questions in blue on your
computer, and answering the questions in red in your
journal.
Space, Click, Right Arrow or swipe left to move to
the next slide.
food.dotPlot is another plot that can be used to analyze a
numerical variable.
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.
(2) Write and run the code for a more accurate
dotPlot by including the nint
option.
nint equal to the maximum value for
sugar minus the minimum value for sugar plus
one.
food data spreadsheet, click
on the sugar header to sort in ascending order (to obtain
minimum). sugar header again to
sort in descending order (to obtain maximum). 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.
(3) Write and run code splitting the dotPlot
displaying the distribution of grams of sugar in two, by
faceting on our observations’ salty_sweet
variable.
R decides which observations
go into the left or right plot.sugar levels of
salty and sweet snacks if the dotPlots were stacked on top of one
another.layout option in our dotPlot
function.
dotPlot split by
salty_sweet.nint option to
add the layout option to the dotPlot
function.Salty and a plot for
Sweet is useful for comparing Salty and
Sweet. What if we want to examine one group by itself?food
dataset containing only Salty snacks. We will break it down
piece by piece in the next few slides.View food_salty and write down
the number of observations in it.R is really just about supplying directions
in a way that R understands.
<- symbolfilter() 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?R what we are really
doing is giving a value, or set of values, a specific name for us to use
later.<- is called the “assignment” operator. It
assigns names (on the left) to values (on the right).
<- symbol.<-) …<-
…food_salty.food_salty to do anything we could do
with the regular food data …
Salty.food_salty,
food_salty now appears in the Environment pane.
Whenever data is assigned to a variable name, that variable name will
appear in the Environment pane.food_salty to make
a dotPlot of the sodium in our
Salty snacks.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.(9) Create a dotPlot and answer the
question: About how much sugar does the typical sweet snack
have?
(10) Create a dotPlot and answer the
question: How does the typical amount of sugar compare when
healthy_level < 3 and when
healthy_level > 3?
Because you are now working with subsets of data, it is important to label our plots and make this distinction.
main option to add a title to our plots.
dotPlot of the sugar in
Sweet snacks.