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.
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).
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/summersimpson/Downloads/VisualizingAmountsProportionsFall2025"
setwd("/Users/summersimpson/Downloads/VisualizingAmountsProportionsFall2025")
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.
library("dplyr")
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.2 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.3.0
## ✔ purrr 1.1.0 ✔ tidyr 1.3.1
## ── 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("forcats")
library("ggplot2")
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"
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
The graph should be based on the favorite sauce variable stored as a numeric variable.
Change the labels on the x-axis to reflect respondents’ favorite sauces in words.
Change the numerical labels on each bar to words that correspond to sauce type. Make sure your labels are readable on the graph, to do this you will need to shorten the labels. When abbreviating labels you want to do so in a way that someone looking at the graph will still know what you are referencing. Change the color of the bars to purple.
ggplot(data = BBQData_Assignment7, #"data" argument
mapping = aes(x = Favorite.Meat)) + #"mapping" and "aesthetics" arguments
geom_bar(fill="purple") + #"geom" argument
labs(x="Favorite Sauce", y="Total") +
scale_x_continuous(breaks = c(1, 2, 3,4,5,6,7), label = c("Eastern", "Western", "Kansas", "Dry Rub", "SC Mustard", "Korean", "Other"))
# 5. Amounts – Bar Graph 2 Create a bar graph that plots respondents’
favorite sauce.
BBQData_Assignment7 %>%
mutate(Favorite.Sauce.Label=NA) %>%
mutate(Favorite.Sauce.Label=replace(Favorite.Sauce.Label,Favorite.Sauce==1,"Eastern")) %>%
mutate(Favorite.Sauce.Label=replace(Favorite.Sauce.Label,Favorite.Sauce==2,"Western")) %>%
mutate(Favorite.Sauce.Label=replace(Favorite.Sauce.Label,Favorite.Sauce==3,"Kansas")) %>%
mutate(Favorite.Sauce.Label=replace(Favorite.Sauce.Label,Favorite.Sauce==4,"Dry Rub")) %>%
mutate(Favorite.Sauce.Label=replace(Favorite.Sauce.Label,Favorite.Sauce==5,"SC Mustard")) %>%
mutate(Favorite.Sauce.Label=replace(Favorite.Sauce.Label,Favorite.Sauce==6,"Korean")) %>%
mutate(Favorite.Sauce.Label=replace(Favorite.Sauce.Label,Favorite.Sauce==7,"Other"))-> BBQData_Assignment7
ggplot(data = BBQData_Assignment7, #"data" argument
mapping = aes(x = Favorite.Sauce.Label)) + #"mapping" and "aesthetics" arguments
geom_bar() #"geom" argument
- Change the labels on the x-axis to reflect respondents’ favorite
sauces in words.
ggplot(data = BBQData_Assignment7, #"data" argument
mapping = aes(x = Favorite.Sauce.Label)) + #"mapping" and "aesthetics" arguments
geom_bar() + #"geom" argument
labs(x="Favorite Sauce", y="Total")
- Change the color of the bars to orange.
ggplot(data = BBQData_Assignment7,
mapping = aes(x = fct_rev(fct_infreq(Favorite.Sauce.Label)))) +
geom_bar(fill="orange") +
labs(x="Favorite Sauce", y="Total")
- Order respondents’ favorite sauces from most to least
ggplot(data = BBQData_Assignment7,
mapping = aes(x = fct_infreq(Favorite.Sauce.Label))) +
geom_bar(fill="orange") +
labs(x="Favorite Sauce", y="Total")
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.
BBQData_Assignment7 %>%
count(Favorite.Sauce.Label)
## Favorite.Sauce.Label n
## 1 Dry Rub 46
## 2 Eastern 162
## 3 Kansas 26
## 4 Korean 27
## 5 Other 30
## 6 SC Mustard 25
## 7 Western 63
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.
BBQData_Assignment7 %>%
count(Favorite.Sauce.Label) -> FavoriteSauceData
head(FavoriteSauceData)
## Favorite.Sauce.Label n
## 1 Dry Rub 46
## 2 Eastern 162
## 3 Kansas 26
## 4 Korean 27
## 5 Other 30
## 6 SC Mustard 25
ggplot(FavoriteSauceData, # data argument
aes(x="", y=n, fill=Favorite.Sauce.Label)) + # mapping and aesthetics arguments
geom_bar(stat="identity", width=1, color="white") + #geom to create bar graph of proportions
coord_polar("y",start=0) #geom argument to transform bar graph to pie chart
- The pie chart should identify the percentage of respondents who prefer
each type of sauce.
FavoriteSauceData %>%
mutate(Percentage.Respondents = n/sum(n)*100) -> FavoriteSauceData
head(FavoriteSauceData)
## Favorite.Sauce.Label n Percentage.Respondents
## 1 Dry Rub 46 12.137203
## 2 Eastern 162 42.744063
## 3 Kansas 26 6.860158
## 4 Korean 27 7.124011
## 5 Other 30 7.915567
## 6 SC Mustard 25 6.596306
FavoriteSauceData %>%
mutate(Percentage.Respondents=round(Percentage.Respondents,1)) -> FavoriteSauceData
head(FavoriteSauceData)
## Favorite.Sauce.Label n Percentage.Respondents
## 1 Dry Rub 46 12.1
## 2 Eastern 162 42.7
## 3 Kansas 26 6.9
## 4 Korean 27 7.1
## 5 Other 30 7.9
## 6 SC Mustard 25 6.6
ggplot(FavoriteSauceData,
aes(x="", y=Percentage.Respondents, fill=Favorite.Sauce.Label)) +
geom_bar(stat="identity", width=1, color="white") +
coord_polar("y",start=0)
ggplot(FavoriteSauceData,
aes(x="", y=Percentage.Respondents, fill=Favorite.Sauce.Label)) +
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))
- The chart should have a “blank” or “clean” background.
ggplot(FavoriteSauceData,
aes(x="", y=Percentage.Respondents, fill=Favorite.Sauce.Label)) +
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")) +
theme_void()
- Change the title on the legend to “Favorite Sauce”.
ggplot(FavoriteSauceData,
aes(x="", y=Percentage.Respondents, fill=Favorite.Sauce.Label)) +
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"))
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)
## Sweetness n
## 1 1 30
## 2 2 83
## 3 3 163
## 4 4 85
## 5 5 18
BBQData_Assignment7 %>%
count(Sweetness) -> SweetnessData
head(SweetnessData)
## Sweetness n
## 1 1 30
## 2 2 83
## 3 3 163
## 4 4 85
## 5 5 18
ggplot(SweetnessData, # data argument
aes(x="", y=n, fill=Sweetness)) + # mapping and aesthetics arguments
geom_bar(stat="identity", width=1, color="white") + #geom to create bar graph of proportions
coord_polar("y",start=0) #geom argument to transform bar graph to pie chart
- The pie chart should identify the percentage of respondents who prefer
each level of sweetness.
SweetnessData %>%
mutate(Percentage.Respondents = n/sum(n)*100) -> SweetnessData
head(SweetnessData)
## Sweetness n Percentage.Respondents
## 1 1 30 7.915567
## 2 2 83 21.899736
## 3 3 163 43.007916
## 4 4 85 22.427441
## 5 5 18 4.749340
SweetnessData %>%
mutate(Percentage.Respondents=round(Percentage.Respondents)) -> SweetnessData
head(SweetnessData)
## Sweetness n Percentage.Respondents
## 1 1 30 8
## 2 2 83 22
## 3 3 163 43
## 4 4 85 22
## 5 5 18 5
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))
- The chart should have a “blank” or “clean” background.
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")) +
theme_void()
# 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 one helped me
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. i didn’t help anyone # 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.