{r} knitr::opts_chunk$set(include = FALSE) r <- getOption("repos") r["CRAN"] <- "http://cran.cnr.berkeley.edu/" options(repos = r)

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).

Identify and Set Your Working Directory

You need to identify and set your working directory in this section.

{r} getwd() #setwd ("Users/katelynlittle/Documents/VisualizingVariablesMaterials)

Installing and Loading Packages and Data Set

You load the packages and data set you’ll use for the lab assignment in this section.

{r} library("dplyr") library("tidyverse") library("openxlsx") library("forcats")

```{r} getwd() #setwd (“VisualizingVariablesData.xlsx”)


# 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.

```{r}
ggplot(data = VisualizingVariablesData,   #"data" argument
       mapping = aes(x = Favorite.Meat)) + #"mapping" and "aesthetics" arguments
  geom_bar()  #"geom" argument

{r} VisualizingVariablesData %>% mutate(Favorite.Meat.Label=NA) %>% mutate(Favorite.Meat.Label=replace(Favorite.Meat.Label,Favorite.Meat==1,"Pulled Pork")) %>% mutate(Favorite.Meat.Label=replace(Favorite.Meat.Label,Favorite.Meat==2,"Pork Ribs")) %>% mutate(Favorite.Meat.Label=replace(Favorite.Meat.Label,Favorite.Meat==3,"Beef Brisket")) %>% mutate(Favorite.Meat.Label=replace(Favorite.Meat.Label,Favorite.Meat==4,"Beef Ribs")) %>% mutate(Favorite.Meat.Label=replace(Favorite.Meat.Label,Favorite.Meat==5,"Poultry")) -> VisualizingVariablesData2

{r} ggplot(data = VisualizingVariablesData2, #"data" argument mapping = aes(x = Favorite.Meat.Label)) + #"mapping" and "aesthetics" arguments geom_bar() #"geom" argument

{r} ggplot(data = VisualizingVariablesData2, mapping = aes(x = fct_infreq(Favorite.Meat.Label))) + geom_bar() + labs(x="Favorite Meat", y="Total")

{r} ggplot(data = VisualizingVariablesData2, mapping = aes(x = fct_rev(fct_infreq(Favorite.Meat.Label)))) + geom_bar() + labs(x="Favorite Meat", y="Total")

{r} ggplot(data = VisualizingVariablesData2, mapping = aes(x = fct_rev(fct_infreq(Favorite.Meat.Label)))) + geom_bar(fill="blue") + labs(x="Favorite Meat", y="Total")

{r} VisualizingVariablesData2 %>% count(Favorite.Meat.Label)

Proportions

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.

{r} VisualizingVariablesData2 %>% count(Favorite.Meat.Label) -> FavoriteMeatData

{r} head(FavoriteMeatData)

{r} ggplot(FavoriteMeatData, # data argument aes(x="", # mapping and aesthetics arguments y=n, fill=Favorite.Meat.Label)) + geom_bar(stat="identity", #geom argument to create bar graph of proportions width=1, color="white") + coord_polar("y", #geom argument to transform bar garph to pie chart start=0)

{r} FavoriteMeatData %>% mutate(Percentage.Respondents = n/sum(n)*100) -> FavoriteMeatData2

{r} head(FavoriteMeatData2)

{r} FavoriteMeatData2 %>% mutate(Percentage.Respondents=round(Percentage.Respondents,1)) -> FavoriteMeatData2

{r} ggplot(FavoriteMeatData2, # data argument aes(x="", # mapping and aesthetics arguments y=Percentage.Respondents, fill=Favorite.Meat.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 Meat"))+ # change legend theme_void() # remove background, grid, numeric labels

Distributions

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.

{r} ggplot(VisualizingVariablesData, #data argument aes(x=Dinner.Plate.Price)) + #mapping and aesthetics arguments geom_histogram() #geom argument

{r} ggplot(VisualizingVariablesData, #data argument aes(x=Dinner.Plate.Price)) + #mapping and aesthetics arguments geom_histogram(fill="red") + #geom argument labs(x="Dinner Plate Price", #changing labels y="Count")

{r} ggplot(VisualizingVariablesData, #data argument aes(x=Dinner.Plate.Price)) + #mapping and aesthetics arguments geom_histogram(fill="red") + #geom argument labs(x="Dinner Plate 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

{r} ggplot(VisualizingVariablesData, #data argument aes(x=Dinner.Plate.Price)) + #mapping and aesthetics arguments geom_histogram(fill="red", #geom argument, specifies color bins = 10) + #specifies bins labs(x="Dinner Plate 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

{r} ggplot(VisualizingVariablesData, #data argument aes(x=Dinner.Plate.Price)) + #mapping and aesthetics arguments geom_histogram(fill="red", #geom argument, specifies color bins = 60) + #specifies bins labs(x="Dinner Plate 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

Publish Document

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.