1. Describe completely using simple terms what the following code chunk does.

parcels <- read.csv("C:/Users/StatLab2 PC7/Desktop/JADENA/STAT 137 EDA/parcels.csv", header=TRUE)
parcels %>% # Using the pipe operator, it makes the parcels data more neat and systematic that improves its efficiency and readability. 
  select(CITY_DECODE, DESIGN_STYLE_DECODE) %>% #Using the select function, it particularly selecting the row that is higly neeeded in the parcels data.
  drop_na(c(CITY_DECODE, DESIGN_STYLE_DECODE)) %>% # As obserevd in the parcels data there are location which has no data or also known as NA where it has missing values. Thus, in this line of code we discard or remove those missing values found in the rows of CITY_DECODE and DESIGN_STYLE_DECODE using the drop_na function.
  filter(DESIGN_STYLE_DECODE == "Townhouse") %>% # Using the filter function it eliminate unnecessary data found on the DESIGN_STYLE_DECODE column and only retains the data which has a DESIGN_STYLE_DECODE of "Townhouse".
  group_by(CITY_DECODE) %>% #By the name itself group_by function groups the data frame according to its CITY_DECODE. 
  summarize(count=n()) %>% #The summarize function, summarizes the number of observation in each type of CITY_DECODE. In in an alphabetical way (A_Z).
  filter(count>1000) %>% #Using the filter function, it eliminates the count which has a value lesser than 1000 and only keeping those CITY_DECODE which has count greater than 1000. 
  mutate (prop= count/ sum(count)) # creating a new column where it has a column name, prop. And geting this new column it gets the ratio between the count and the sum of count.
## By utilizing the different function such as the select, drop_na, filter, group_by, summarize, and mutate to the parcels data, we are able to make a tibble which has 6 rows and 3 columns. In each rows it is the specific city with at least 1,000 parcels classified as a “Townhouse” having a count greater than 1000 and getting it proportion to each of the number of counts.

2. Open the starwars data set in the dplyr package.

starwars

a. Create a barplot of the number of starwars characters with either brown or black hair color. Color each bar based on the sex of the character. Make sure to remove NAs in the data before creating the plot. Also, provide the appropriate labels of the axes. Describe the plot.

starwars %>% 
  select(hair_color, sex) %>%
  filter(hair_color %in% c("brown", "black")) %>% 
  drop_na(c(hair_color,sex)) %>%
  group_by(sex, hair_color) %>%
  summarize(count = n()) %>% 
  ggplot(aes(x = hair_color, y = count, fill = sex)) + 
  geom_bar(position=position_dodge(), stat="identity")+
  geom_text(aes(label = count), vjust = -0.3, color="black",
            position=position_dodge(0.9), size=3.0)+
  scale_y_continuous(expand=c(0,0), limits=c(0,12))+
  labs(x="Hair Color", y="Number of Starwars Character")+
  theme_classic()
## `summarise()` has grouped output by 'sex'. You can override using the `.groups`
## argument.

## The table shows that both black and brown hair color are famous among the male starwars characters. The brown hair color is dominant among the male than the female starwars characters. For the black hair color there are 9 males and 3 females, and forthe brown hair color there are 11 males and 6 females.

b. Create a barplot of the means of the mass of each starwars character categorized according to their eye color and sex. Indicate the means on top of each bar. Make sure to remove NAs in the data before creating the plot. Also, provide the appropriate labels of the axes. Describe the plot

starwars %>%  
  select(eye_color, sex, mass) %>%
  filter(sex %in% c("male", "female")) %>% 
  filter(eye_color %in% c("black", "brown", "blue","yellow")) %>% 
  drop_na(c(eye_color, sex, mass)) %>% 
  group_by(eye_color, sex) %>%
  summarize(m = mean(mass)) %>% 
  ggplot(aes(x = eye_color, y = m , fill = sex), bins=50,) + 
  geom_bar(position=position_dodge(), stat="identity")+
  geom_text(aes(label=sprintf("%0.1f", round(m, digits= 1))), vjust = -0.3,
            color="black", position=position_dodge(0.9), size=3.0,)+
  labs(x="Eye Color", y="Average Mass of Starwars Characters")+
  theme_classic()+
  scale_y_continuous(expand=c(0,0), limits=c(0,110))
## `summarise()` has grouped output by 'eye_color'. You can override using the
## `.groups` argument.

# The table shows the Eye color of the Starwars characters in accordance to their average mass. Both for male and female, the greatest average mass are those who has blue eye color, while the least average mass are those who has brown eye color.In every kind of eye color, males surpasses females in line with the average mass.