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()
setwd(“/Users/corddoss/Desktop/Research methods class/Week
7/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.
library(“dplyr”) library(“tidyverse”) library(“openxlsx”) >
library(“forcats”) > library(“ggplot2”) VisualizingBBQData <-
read.xlsx(“BBQData_Assignment7.xlsx”) names(VisualizingBBQData) ## [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 =
VisualizingBBQData, + mapping = aes (x = Favorite.Sauce)) + +
geom_bar(fill = “purple”) + + 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”)) - 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.
Create a bar graph that plots respondents’ favorite sauce.
VisualizingBBQData %>% + 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”)) -> VisualizingBBQData
ggplot(data = VisualizingBBQData, + mapping = aes(x = fct_infreq(Favorite.Sauce.Label))) + + geom_bar(fill = “orange”) + + labs(x=“Favorite Sauce”, y=“Total”) - Recode the numeric format of the variable into a variable with a character format. - Use the character format version of the variable to construct the graph. - Change the labels on the x-axis to reflect respondents’ favorite sauces in words. - Change the color of the bars to orange. - Order respondents’ favorite sauces from most to least ggplot(data = VisualizingBBQData, + mapping = aes(x = fct_infreq(Favorite.Sauce.Label))) + + geom_bar(fill = “orange”) + + 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. Sauce.Totals <- VisualizingBBQData %>% + count(Favorite.Sauce.Label) head(Sauce.Totals) # 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.
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. VisualizingBBQData %>% count(Sweetness) -> SweetnessData head(SweetnessData) SweetnessData %>% mutate(Percentage.Sweetness = n/sum(n)*100) -> SweetnessData SweetnessData %>% mutate(Percentage.Sweetness=round(Percentage.Sweetness,0)) -> SweetnessData SweetnessData %>% mutate(Sweetness.Label = case_when( Sweetness == 1 ~ “Not at all Sweet”, Sweetness == 2 ~ “Slightly Sweet”, Sweetness == 3 ~ “Moderately Sweet”, Sweetness == 4 ~ “Very Sweet”, Sweetness == 5 ~ “Extremely Sweet” )) -> SweetnessData ggplot(SweetnessData, aes(x=““, y=Percentage.Sweetness, fill=Sweetness.Label)) + geom_bar(stat=”identity”, width=1, color=“white”) + coord_polar(“y”,start=0) + geom_text(aes(label = Percentage.Sweetness), position = position_stack(vjust = 0.5)) + guides(fill=guide_legend(“Preferred Sweetness”)) + theme_void()
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
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.
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.