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).
getwd() setwd( “C:/Users/giova/AppData/Local/Temp/b8c09d1a-52ba-4790-b0e7-df6768195ef3_VisualizingVariablesMaterials.zip.ef3/VisualizingVariablesMaterials”)
install.packages(“ggplot2”)
install.packages(“dplyr”)
install.packages(“readr”)
library(ggplot2) library(dplyr)
library(readr)
data <- read_csv(“path_to_your_file.csv”)
head(data) summary(data)
ggplot(data, aes(x = ““, y = Percentage.Respondents, fill = Favorite.Meat.Label)) + geom_col(width = 1, color =”white”) + geom_text(aes(label = Percentage.Respondents), position = position_stack(vjust = 0.5)) + coord_polar(“y”, start = 0) + theme_void() # Amounts You need to create a set of graphs and identify respondents’ preferences in this subsection. You need to create two bar graphs that plot respondents’ favorite sauce. The first should be a basic bar graph. The second should order respondents’ favorite sauces from most to least and change the color of the bars to orange. For both bar graphs, you should change the labels on the x-axis to reflect respondents’ favorite sauces in words. After you’ve done that, you need to identify how many total respondents prefer each type of sauce.
library(ggplot2) library(dplyr)
ggplot(VisualizingVariablesData, aes(x = Favorite.Sauce)) + geom_bar(fill = “steelblue”) + labs(x = “Favorite BBQ Sauce”, y = “Number of Respondents”, title = “Respondents’ Favorite BBQ Sauces”) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplot(VisualizingVariablesData, aes(x = reorder(Favorite.Sauce, Favorite.Sauce, FUN = length))) + geom_bar(fill = “orange”) + labs(x = “Favorite BBQ Sauce”, y = “Number of Respondents”, title = “Respondents’ Favorite BBQ Sauces (Ordered)”) + theme(axis.text.x = element_text(angle = 45, hjust = 1))
sauce_counts <- VisualizingVariablesData %>% group_by(Favorite.Sauce) %>% summarise(Respondents = n())
print(sauce_counts) sauce_counts <- VisualizingVariablesData %>% group_by(Favorite.Sauce) %>% summarise(Respondents = n())
print(sauce_counts)
You need to create two pie charts in this section: one that plots respondents’ favorite sauces and one that plots respondents’ preferred level of sweetness. Both pie charts should identify what percentage of respondents prefer each type of sauce or level of sweetness (rounded to one decimal point), respectively, on the appropriate segment or slice of the pie chart. Both charts should have a “blank” or “clean” background. You should also identify how many people (total) prefer each level of sweetness.
sauce_data <- VisualizingVariablesData %>% group_by(Favorite.Sauce) %>% summarise(Respondents = n()) %>% mutate(Percentage.Respondents = round(Respondents / sum(Respondents) * 100, 1))
ggplot(sauce_data, aes(x = ““, y = Percentage.Respondents, fill = Favorite.Sauce)) + geom_col(width = 1, color =”white”) + geom_text(aes(label = paste(Percentage.Respondents, “%”)), position = position_stack(vjust = 0.5)) + coord_polar(“y”, start = 0) + theme_void() + # Clean background labs(title = “Respondents’ Favorite BBQ Sauces”) sweetness_data <- VisualizingVariablesData %>% group_by(Sweetness) %>% summarise(Respondents = n()) %>% mutate(Percentage.Respondents = round(Respondents / sum(Respondents) * 100, 1))
ggplot(sweetness_data, aes(x = ““, y = Percentage.Respondents, fill = factor(Sweetness))) + geom_col(width = 1, color =”white”) + geom_text(aes(label = paste(Percentage.Respondents, “%”)), position = position_stack(vjust = 0.5)) + coord_polar(“y”, start = 0) + theme_void() + # Clean background labs(title = “Respondents’ Preferred Sweetness Level”) # Print the number of respondents for each sweetness level sweetness_counts <- VisualizingVariablesData %>% group_by(Sweetness) %>% summarise(Respondents = n())
print(sweetness_counts)
You need to create two histograms in this section: one that plots the distribution of how much respondents will pay for a plate of ribs and one that plots the distribution of how much respondents will pay for a pulled pork sandwich. Both histograms should have the following features: purple bars, 15 bins, no background grid, no background color, and x- and y-axes. You should also identify the mean and standard deviation of how much respondents will pay for a plate of ribs and how much they are willing to pay for a pulled pork sandwich.
ggplot(VisualizingVariablesData, aes(x = Ribs.Price)) + geom_histogram(bins = 15, fill = “purple”, color = “white”, alpha = 0.7) + theme_minimal() + theme(panel.grid = element_blank(), plot.background = element_blank()) + labs(title = “Distribution of How Much Respondents Will Pay for a Plate of Ribs”, x = “Price (in dollars)”, y = “Frequency”) + # Add mean and standard deviation text annotate(“text”, x = mean(VisualizingVariablesData\(Ribs.Price, na.rm = TRUE), y = max(table(VisualizingVariablesData\)Ribs.Price), na.rm = TRUE), label = paste(“Mean: \(", round(mean(VisualizingVariablesData\)Ribs.Price, na.rm = TRUE), 2),”: \(", round(sd(VisualizingVariablesData\)Ribs.Price, na.rm = TRUE), 2)), color = “black”, size = 4, vjust = -1.5)
ggplot(VisualizingVariablesData, aes(x = Sandwich.Price)) + geom_histogram(bins = 15, fill = “purple”, color = “white”, alpha = 0.7) + theme_minimal() + theme(panel.grid = element_blank(), plot.background = element_blank()) + labs(title = “Distribution of How Much Respondents Will Pay for a Pulled Pork Sandwich”, x = “Price (in dollars)”, y = “Frequency”) + # Add mean and standard deviation text annotate(“text”, x = mean(VisualizingVariablesData\(Sandwich.Price, na.rm = TRUE), y = max(table(VisualizingVariablesData\)Sandwich.Price), na.rm = TRUE), label = paste(“Mean: \(", round(mean(VisualizingVariablesData\)Sandwich.Price, na.rm = TRUE), 2),”: \(", round(sd(VisualizingVariablesData\)Sandwich.Price, na.rm = TRUE), 2)), color = “black”, size = 4, vjust = -1.5)
korean_style_count <- VisualizingVariablesData %>% filter(Favorite.Sauce == “Korean style”) %>% summarise(Respondents = n())
print(korean_style_count)
cat(“Standard deviation for Rib Plate:”, round(rib_plate_sd, 2), “”) cat(“Standard deviation for Pulled Pork Sandwich:”, round(sandwich_sd, 2), “”)
print(korean_style_count)
Click the “Knit” button to publish your work as an html document. This document or file will appear in 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.