Overview

This file contains a set of tasks that you need to complete in R for the lab assignment. The tasks may require you to add a code chuck, type code into a chunk, and/or execute code. Don’t forget that you need to acknowledge if you used any resources beyond class materials or got help to complete the assignment.

Additional information and examples relevant to this assignment can be found in the file “VisualizingAmountsandProportionsTutorial.html”.

The data set you will use is different than the one used in the instructions. Pay attention to the differences in the Excel files name, any variable names, and/or object names. You will need to adjust your code accordingly.

Once you have completed the assignment, you will need to knit this R Markdown file to produce an html file. You will then need to upload the .html file and this .Rmd file to AsULearn.

1. Add your Name and the Date

The first thing you need to do in this file is to add your name and date in the lines underneath this document’s title (see the code in lines 9 and 10).

Author: Jacob Stockton Date: 10-1-25

2. Identify and Set Your Working Directory

You need to identify and set your working directory in this section. If you are working in the cloud version of RStudio, enter a note here to tell us that you did not need to change the working directory because you are working in the cloud.

getwd()
## [1] "/Users/jacobstockton/Desktop/Research methods Lab/VisualizingAmountsProportionsFall2025"
setwd("/Users/jacobstockton/Desktop/Research methods Lab/VisualizingAmountsProportionsFall2025")

3. Installing and Loading Packages and Data Set

You load the packages and data set you’ll use for the lab assignment in this section. In this lab we will use the packages: dyplr, tidyverse, forcats, ggplot2, and openxlsx.

install.packages("ggplot2")
## 
## The downloaded binary packages are in
##  /var/folders/3j/tn1f_gtn4674lr97v0dkxc5c0000gn/T//RtmpOnaHJ8/downloaded_packages
install.packages("forcats")
## 
## The downloaded binary packages are in
##  /var/folders/3j/tn1f_gtn4674lr97v0dkxc5c0000gn/T//RtmpOnaHJ8/downloaded_packages
library(forcats)
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.3.0
## ✔ purrr     1.1.0     ✔ tidyr     1.3.1
## ✔ readr     2.1.5     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openxlsx)
library(dplyr)
BBQData_Assignment7 <- read.xlsx("BBQData_Assignment7.xlsx")
names(BBQData_Assignment7)
##  [1] "Observation"        "Sex"                "Age"               
##  [4] "Hometown"           "Favorite.Meat"      "Favorite.Sauce"    
##  [7] "Sweetness"          "Favorite.Side"      "Restaurant.City"   
## [10] "Restaurant.Name"    "Minutes.Driving"    "Sandwich.Price"    
## [13] "Dinner.Plate.Price" "Ribs.Price"

4. Amounts – Bar Graph 1

Create a bar graph that plots respondents’ favorite sauce.

ggplot(data = BBQData_Assignment7,   #"data" argument
       mapping = aes(x = Favorite.Sauce)) + #"mapping" and "aesthetics" arguments
  geom_bar() + #"geom" argument
  labs(x="Favorite Sauce",  y="Total")

ggplot(data = BBQData_Assignment7,   
       mapping = aes(x = Favorite.Sauce)) + 
  geom_bar() +
  labs(x = "Favorite Sauce", y = "Total") +
  scale_x_discrete(labels = c(
    "1" = "Eastern style (with no tomato)",
    "2" = "Western style (with tomato)",
    "3" = "Kansas style (with molasses)",
    "4" = "Dry Rub",
    "5" = "South Carolina Mustard",
    "6" = "Korean style",
    "7" = "Other"
  ))

ggplot(data = BBQData_Assignment7, 
       mapping = aes(x = Favorite.Sauce)) + 
  geom_bar() + 
  labs(x = "Favorite Sauce", y = "Total") +
  scale_x_discrete(labels = c("East", "West", "Kansas", "Dry Rub", 
                              "SC Mustard", "Korean", "Other"))

BBQData_Assignment7$Favorite.Sauce <- as.factor(BBQData_Assignment7$Favorite.Sauce)
ggplot(data = BBQData_Assignment7,   
       mapping = aes(x = fct_rev(fct_infreq(Favorite.Sauce)))) + 
  geom_bar(fill="purple")  +
  labs(x="Favorite Sauce", y="Total")

5. Amounts – Bar Graph 2

Create a bar graph that plots respondents’ favorite sauce.

ggplot(data = BBQData_Assignment7,   #"data" argument
       mapping = aes(x = Favorite.Sauce)) + #"mapping" and "aesthetics" arguments
  geom_bar() + #"geom" argument
  labs(x="Favorite Sauce",  y="Total")

BBQData_Assignment7 %>% mutate(Favorite.Sauce=NA) %>% mutate(Favorite.Sauce=replace(Favorite.Sauce,Favorite.Sauce==1,"Western style (with tomato)")) %>% mutate(Favorite.Sauce=replace(Favorite.Sauce,Favorite.Sauce==2,"Eastern style (with no tomato)")) %>% mutate(Favorite.Sauce=replace(Favorite.Sauce,Favorite.Sauce==3,"South Carolina Mustard")) %>% mutate(Favorite.Sauce=replace(Favorite.Sauce,Favorite.Sauce==4,"Kansas style (with molasses)")) %>% mutate(Favorite.Sauce=replace(Favorite.Sauce,Favorite.Sauce==5,"Korean Style")) %>% mutate(Favorite.Sauce=replace(Favorite.Sauce,Favorite.Sauce==6,"Dry Rub")) %>%mutate(Favorite.Sauce=replace(Favorite.Sauce,Favorite.Sauce==7,"Other")) -> BBQData
ggplot(data = BBQData_Assignment7,   #"data" argument
       mapping = aes(x = Favorite.Sauce)) + #"mapping" and "aesthetics" arguments
  geom_bar() + #"geom" argument
labs(x="Favorite Sauce",  y="Total") 

ggplot(data = BBQData_Assignment7, 
       mapping = aes(x = Favorite.Sauce)) + 
  geom_bar() + 
  labs(x = "Favorite Sauce", y = "Total") +
  scale_x_discrete(labels = c("East", "West", "Kansas", "Dry Rub", 
                              "SC Mustard", "Korean", "Other"))

BBQData_Assignment7$Favorite.Sauce <- as.factor(BBQData_Assignment7$Favorite.Sauce)
ggplot(data = BBQData_Assignment7,   
       mapping = aes(x = fct_rev(fct_infreq(Favorite.Sauce)))) + 
  geom_bar(fill="orange")  +
  labs(x="Favorite Sauce", y="Total")

ggplot(data = BBQData_Assignment7,   
       mapping = aes(x = fct_infreq(Favorite.Sauce))) + 
  geom_bar()  +
  labs(x="Favorite Sauce", y="Total")

6. Total respondents that perfer each type of suace.

Identify how many total respondents prefer each type of sauce. Save the count as a data object. Use the head() command to display the data.

FavoriteSauceData <- BBQData_Assignment7 %>%
  count(Favorite.Sauce)
head(FavoriteSauceData)
##   Favorite.Sauce   n
## 1              1 162
## 2              2  63
## 3              3  26
## 4              4  46
## 5              5  25
## 6              6  27

7. Proportions - Pie Chart 1

Create a pie chart that plots the respondents’ favorite sauce. You will need to transform the counts you calculated above into percentages and save these as a new data object before you can graph the data.

FavoriteSauceData %>%
  mutate(Percentage.Respondents = n/sum(n)*100) -> FavoriteSauceData
head(FavoriteSauceData)
##   Favorite.Sauce   n Percentage.Respondents
## 1              1 162              42.744063
## 2              2  63              16.622691
## 3              3  26               6.860158
## 4              4  46              12.137203
## 5              5  25               6.596306
## 6              6  27               7.124011
FavoriteSauceData %>%
  mutate(Percentage.Respondents=round(Percentage.Respondents,1)) -> FavoriteSauceData
ggplot(FavoriteSauceData,                      
  aes(x="", y=Percentage.Respondents, fill=Favorite.Sauce)) +
  geom_bar(stat="identity", width=1,  color="white") + 
  coord_polar("y",start=0) 

ggplot(FavoriteSauceData,                      
  aes(x="", y=Percentage.Respondents, fill=Favorite.Sauce)) +
  geom_bar(stat="identity", width=1,  color="white") + 
  coord_polar("y",start=0) +
  geom_text(aes(label = Percentage.Respondents), position = position_stack(vjust = 0.5))

ggplot(FavoriteSauceData,                      
  aes(x="", y=Percentage.Respondents, fill=Favorite.Sauce)) +
  geom_bar(stat="identity", width=1,  color="white") + 
  coord_polar("y",start=0) +
  geom_text(aes(label = Percentage.Respondents), position = position_stack(vjust = 0.5)) +
  guides(fill=guide_legend("Favorite Sauce")) 

8. Proportions - Pie Chart 2

Create a pie chart that plots the respondents’ preferred level of sweetness. You need to identify how many total respondents prefer each type of sauce. Save the count as a data object. Then transform the count into percentages and save these as a new data object before you can graph the data.

BBQData_Assignment7 %>%
  count(Sweetness) -> SweetnessData
head(SweetnessData)
##   Sweetness   n
## 1         1  30
## 2         2  83
## 3         3 163
## 4         4  85
## 5         5  18
SweetnessData <- SweetnessData %>%
  rename(Percentage.Respondents = `n`)
if ("Percentage.Respondents" %in% names(SweetnessData)) {
  SweetnessData <- SweetnessData %>%
    mutate(Percentage.Respondents = round(Percentage.Respondents, 0))
}
ggplot(SweetnessData,                      
  aes(x="", y=Percentage.Respondents, fill=Sweetness)) +
  geom_bar(stat="identity", width=1,  color="white") + 
  coord_polar("y",start=0) 

ggplot(SweetnessData,                      
  aes(x="", y=Percentage.Respondents, fill=Sweetness)) +
  geom_bar(stat="identity", width=1,  color="white") + 
  coord_polar("y",start=0) +
  geom_text(aes(label = Percentage.Respondents), position = position_stack(vjust = 0.5)) +
  guides(fill=guide_legend("Sweetness"))

9. Did you recieve help?

Enter the names of anyone one that assisted you with completing this lab. If no one helped you complete the assignment, just type out that no one helped you. - No

10. Did you provide anyone help with completing this lab?

Enter the names of anyone that you assisted with completing this lab. If you did not help anyone, then just type out that you didn’t help anyone. - Summer Simpson # 11. Knit the Document Click the “Knit” button to publish your work as an html document. This document or file will appear in the folder specified by your working directory. You will need to upload both this RMarkdown file and the html file it produces to AsU Learn to get all of the lab points for this week.