Directions

The objective of this assignment is to complete and explain basic plots before moving on to more complicated ways to graph data.

Each question is worth 5 points.

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Canvas. Please make sure that this link is hyper linked and that I can see the visualization and the code required to create it (echo=TRUE).

Questions

  1. For The following questions use the Marriage data set from the mosaicData package.
ggplot(Marriage, aes(x = college, fill = race)) +
  geom_histogram(binwidth = 1, position = 'dodge2') +
  labs(x = 'College Duration', y = 'Head Count')
## Warning: Removed 10 rows containing non-finite values (`stat_bin()`).

#Summary of race
summary(Marriage$race)
## American Indian           Black        Hispanic           White 
##               1              22               1              74

Based on the above histogram plot, we can say that the time spent by white people in college is greater than all the other races. On an average, most of the races spent up-to 4 years in college. We can assume that the ‘Marriage’ dataset is heavy on white and black people data compared to the other races.

ggplot(Marriage, aes(x = age, y = college)) + 
  geom_point(aes(color = sign, shape = race), size = 1.5)  +
  labs(x = 'Age', y = 'College Duration') +
  facet_wrap(~person)
## Warning: Removed 10 rows containing missing values (`geom_point()`).

#Five variables defined in the graph are mentioned below:
#1. X-axis represents age
#2. Y-axis represents years spent in college 
#3. Plot is faceted (one for bride and the other for groom)
#4. Shapes represent race 
#5. Colors represent zodiac sign 

Your objective for the next four questions will be write the code necessary to exactly recreate the provided graphics.

  1. Boxplot Visualization

This boxplot was built using the mpg dataset. Notice the changes in axis labels.

ggplot(mpg, aes(manufacturer, hwy)) +
  geom_boxplot() + 
  labs(x = 'Vehicle Manufacturer', y = 'Highway Fuel Efficiency (mpg)') + 
  coord_flip() + 
  theme_minimal()

  1. Stacked Density Plot

This graphic is built with the diamonds dataset in the ggplot2 package.

ggplot(diamonds, aes(price, fill = cut, color = cut)) + 
  geom_density(alpha = 0.4, size = 0.4) +
  scale_fill_discrete() +
  labs(x = 'Diamond Price (USD)', y = 'Density', title = 'Diamond Price Based on Density') +
  theme_minimal()

  1. Sideways bar plot

This graphic uses the penguins dataset and shows the counts between males and females by species.

ggplot(penguins, aes(x = sex, fill = species)) +
  geom_bar(alpha = 0.9) +
  scale_fill_brewer(palette = 'Set1') +
  facet_wrap(~species, ncol = 1) +
  coord_flip() +
  labs(x = 'Gender', y = 'Gender Count') +
  theme_minimal()

  1. Scatterplot

This figure examines the relationship between bill length and depth in the penguins dataset.

ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
  geom_point() +
  geom_smooth(method = 'lm', se = FALSE, aes(color = species)) +
  labs(col = 'Species', x = 'Bill Length (mm)', y = 'Bill Depth (mm)',
    title = 'Scatterplot Between Bill Length & Depth By Species')