library(tidyverse)Lab 4
Due Friday, Feb 13 at 11:59pm.
Set up
Open your RStudio project by going to File > Open Project. Select your BIOS 600 folder. (If you see a blue cube with your project name at the top right of your RStudio window, you’re good to go - you are already in your project folder.)
To begin, start a new Quarto file. Change the title to Lab 4. Keep the output as html.
“Save As” into your class project folder with a new name: “
lab-04.qmd”.Replace “Your Name Here” with your name in the YAML (the header of the document). Also, replace “Today’s Date Here” with today’s date. You may delete everything under the YAML.
Render your document to make sure your YAML is rendering correctly.
Write all code and narrative in your Quarto file. Write all narrative in complete sentences. Throughout the assignment, you should periodically render your Quarto document to produce the updated html file.
Load the tidyverse
Load in the tidyverse package for today’s lab using the code below:
Practice with ggplot and tidyverse
Today’s lab will is more practice with ggplot and the tidyverse. You will also learn about loading data into R and setting a working directory to look for data.
Fast foods have been criticized as offering unhealthy foods which are calorically dense, high in sodium and added sugars, and low in vitamins and minerals. Today’s dataset was compiled by Joey Stanley at BYU and consists of information regarding McDonald’s menu items.
When performing data visualization, make sure that all your plots have clear and informative titles and axis labels. When investigating more complex relationships with many variables, this simple tip will save you and your readers a lot of time and confusion.
Loading in data
Previously, you loaded data directly into R by using the read.csv command that pointed to a website. But what if you had a dataset on your computer?
You can still do so, but need to point R to reference the appropriate space on your computer. For instance, if I downloaded the McDonald’s Menu Items dataset to my Downloads folder and named it file.csv, I might be able to load the dataset into R under the name mcd with the following command:
mcd <- read.csv("C:/Users/Kara/Downloads/file.csv")Note that file.csv, the name of the file on your computer, could be different from the object name in R (this case, mcd).
However, file paths can be quite long. There’s a better way! This is where our project folder comes in.
First, make sure you are in your project folder. You should see a blue cube at the top right of your RStudio window with the name of your project. If you see it, great. If not, you can open your project with File > Open Project.
At the beginning of class, you should have created a data folder that lives in your project folder. If you copy your data (file.csv) into your data folder, you can read it in with the following code:
mcd <- read.csv("data/file.csv")Because we are working out of our project folder, R knew to start looking in the data folder within the project by default. Using this strategy provides a shortcut instead of needing to provide the entire file path.
Any R output will go into your project folder (for instance, if you want to specifically save plots, figures, etc.) by default.
Note: there are some syntax issues with the way R parses in slashes, and this may change from Mac to PC (…anyone use Linux?). You can always check your current working directory with the following command in order to check the formatting of the file path (and also see what the current directory is!):
getwd()[1] "C:/Users/nwiec/OneDrive/Documents/GitHub/BIOS.600.602/labs"
Downloading the data
First, download the Joey Stanley dataset. The data are available on his website here (https://joeystanley.com/data/), and clicking on “McDonald’s Menu Items. Save the data in your data folder within your project folder as menu.csv.
On your own!
Exercise 1
Load in the McDonald’s dataset from Joey Stanley’s website (save the file to your computer). Watch out for the naming of the file and where it’s saved on your computer! Print the first seven rows of the dataset with
slice().Print the column names in your dataset with
names().
Exercise 2
Create a table which displays the number of menu items in each food category. (Hint: use the count() function.) Which category has the most number of items? Which category has the least?
Exercise 3
- Modify the code below to create a new variable called
cal_ozwhich calculates the calories per ounce in each of the menu items. In this code, we are saving the dataset to a new object calledmcd_cal.
mcd_cal <- mcd |>
mutate(_______) # create the new variable here- Create a new R Chunk. Now, within your newly created dataset
mcd_cal, select the variablesCategory,Item, andcal_oz, then print the first five observations withslice(). Within the first five observations, which food item has the highest calories per ounce?
Exercise 4
- Create a table which displays the top five most caloric menu items (only based on total calories), with the highest-value item at the top of the table. In doing so, display only the name of the menu item and the calories. You may use the following code to get started:
mcd_cal |>
select(_____, _____) |>
arrange(_______) |> # arrange highest to lowest
slice(_______)Now, using similar code as above, create a second table which displays the top five most caloric menu items per ounce (based on the new variable you created in Exercise 2).
Which item ranks the highest in each of the tables? Are you surprised?
Exercise 5
There are 9 calories per gram of fat. Create a new variable which corresponds to the proportion of calories that come from fat for each of the menu items. (Assign this dataset, with the newly created variable, to the object mcd_cal.) You can define the new variable as follows: prop_cal_fat = Fat*9/Calories. (Nothing needs to be printed out here. Just show your code.)
Exercise 6
Create a visualization that shows the distribution of calories in McDonald’s menu items by category. Use the following code to get started, filling in the blanks. (You do not need to alter the geom_boxplot or the theme() parts of the code.)
ggplot(data = _______,
mapping = aes(y = ________,
x = _________,
fill = ________)) +
geom_boxplot() +
theme(legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "___________")What patterns do you see?
Exercise 7
Create a visualization that plots the calories per ounce of items (x-axis) by the proportion of calories that come from fat (y-axis). In this visualization, color by the food category. What pattern(s) do you see?
Submission
As you’ve seen previously, we can Render the template into an .html file that can be opened by any web browser. To export it as a .pdf, open the file in your web browser and then print to or save as a .pdf document. Your TAs will show you how if you need help! (There is a way to directly knit to a .pdf file, but it’s quite a bit more involved.)
You will submit the PDF documents for labs and homework to Gradescope as part of your final submission.
To submit your assignment:
Access Gradescope through the menu on the BIOS 600 Canvas site.
Click on the assignment, and you’ll be prompted to submit it.
Mark the pages associated with each exercise. All of the pages of your lab should be associated with at least one question (i.e., should be “checked”).
Select the first page of your .PDF submission to be associated with the “Formatting” section.
Grading
| Component | Points |
|---|---|
| Ex 1 | 3 |
| Ex 2 | 2 |
| Ex 3 | 3 |
| Ex 4 | 3 |
| Ex 5 | 1 |
| Ex 6 | 3 |
| Ex 7 | 3 |
| Formatting | 3 |
| Total | 21 |
The “Formatting” grade is to assess the document format. This includes having a neatly organized document (no excessive output, warnings/messages when loading packages and/or data) with readable code and your name and the date updated in the YAML.