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(“/Users/Lance/Desktop/Methods Lab/Week7”)
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 ─────────────────── ✔ forcats 1.0.0 ✔ readr 2.1.5 ✔ ggplot2 3.5.1 ✔ stringr 1.5.1 ✔ lubridate 1.9.3 ✔ tibble 3.2.1 ✔ purrr 1.0.2 ✔ tidyr 1.3.1 ── Conflicts ────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() ℹ Use the conflicted package to force all conflicts to become errors library(“openxlsx”) library(“forcats”)
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.
VisualizingVariablesData <- VisualizingVariablesData %>% + mutate(Favorite.Sauce.Label = case_when( + Favorite.Sauce == 1 ~ “Eastern style (with no tomato)”, + Favorite.Sauce == 2 ~ “Western style (with tomato)”, + Favorite.Sauce == 3 ~ “Kansas style (with molasses)”, + Favorite.Sauce == 4 ~ “Dry Rub”, + Favorite.Sauce == 5 ~ “South Carolina Mustard”, + Favorite.Sauce == 6 ~ “Korean style”, + Favorite.Sauce == 7 ~ “Other”, + TRUE ~ NA_character_ + )) ggplot(data = VisualizingVariablesData, mapping = aes(x = Favorite.Sauce.Label)) + geom_bar() ggplot(data = VisualizingVariablesData,
+ mapping = aes(x = fct_rev(fct_infreq(Favorite.Sauce.Label)))) + + geom_bar(fill=“Orange”) + + labs(x=“Favorite Sauce”, + y=“Total”) VisualizingVariablesData %>% + count(Favorite.Sauce.Label) Favorite.Sauce.Label n 1 Dry Rub 46 2 Eastern style (with no tomato) 162 3 Kansas style (with molasses) 26 4 Korean style 27 5 Other 30 6 South Carolina Mustard 25 7 Western style (with tomato) 63
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.
ggplot(FavoriteMeatData2, # data argument + aes(x=““, # mapping and aesthetics arguments + y=Percentage.Respondents, + fill=Favorite.Sauce.Label)) + + geom_col( #geom argument to create bar graph + width=1, + color=”white”) +
+ geom_text(aes(label = Percentage.Respondents), #geom argument to put percentages on bars/slices + position = position_stack(vjust = 0.5)) + + coord_polar(“y”, ##geom argument to transform bar garph to pie chart + start=0) + + guides(fill=guide_legend(“Favorite BBQ Sauce”))+ # change legend + theme_void() # remove background, grid, numeric labels head(FavoriteMeatData2) Favorite.Sauce.Label n Percentage.Respondents 1 Dry Rub 46 12.1 2 Eastern style (with no tomato) 162 42.7 3 Kansas style (with molasses) 26 6.9 4 Korean style 27 7.1 5 Other 30 7.9 6 South Carolina Mustard 25 6.6
VisualizingVariablesData %>% + count(Sweetness) -> SweetnessData head(SweetnessData) Sweetness n 1 1 30 2 2 83 3 3 163 4 4 85 5 5 18 SweetnessData %>% + mutate(Percentage.Respondents = n/sum(n)*100) -> SweetnessData2 head(SweetnessData2) 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 SweetnessData2 %>% + mutate(Percentage.Respondents=round(Percentage.Respondents,1)) -> SweetnessData2
ggplot(SweetnessData2, # data argument + aes(x=““, # mapping and aesthetics arguments + y=Percentage.Respondents, + fill=Sweetness)) + + geom_col( #geom argument to create bar graph + width=1, + color=”white”) +
+ geom_text(aes(label = Percentage.Respondents), #geom argument to put percentages on bars/slices + position = position_stack(vjust = 0.5)) + + coord_polar(“y”, ##geom argument to transform bar garph to pie chart + start=0) + + guides(fill=guide_legend(“Preffered Sweetness”))+ # change legend + theme_void() # remove background, grid, numeric labels
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, #data argument + aes(x = Sandwich.Price)) + #mapping and aesthetics arguments + geom_histogram(fill=“purple”, #geom argument, specifies color + bins = 15) + #specifies bins + labs(x=“Pork Sandwich Price”, #changing labels + y=“Count”) + + theme(panel.grid.major = element_blank(), #getting rid of grid lines and background color + panel.grid.minor = element_blank(), + panel.background = element_blank(), + axis.line = element_line()) # keeping axis line
ggplot(VisualizingVariablesData, aes(x = Ribs.Price)) + # data argument and mapping + geom_histogram(fill = “purple”, bins = 15) + # geom argument, specifies color and number of bins + labs( + title = “Distribution of Ribs Price”, + x = “Price of Ribs”, + y = “Count” + )
mean_ribs_price <- mean(VisualizingVariablesData$Ribs.Price, na.rm = TRUE) print(mean_ribs_price) [1] 23.06
sd_ribs_price <- sd(VisualizingVariablesData$Ribs.Price, na.rm = TRUE) print(sd_ribs_price) [1] 9.94
mean_sandwich_price <- mean(VisualizingVariablesData\(Sandwich.Price, na.rm = TRUE) print(mean_sandwich_price) [1] 13.47 sd_sandwich_price <- sd(VisualizingVariablesData\)Sandwich.Price, na.rm = TRUE) print(sd_sandwich_price) [1] 5.31
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.