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.
marriage_data <- Marriage
ggplot(marriage_data, aes(x = age, fill = race)) + 
  geom_histogram(binwidth = 1, position = "dodge")

#Here we are plotting age vs race of married people. Only the white race got married over the age of 60.

ggplot(data = marriage_data, aes(x = age, y = college)) + 
  geom_point(aes(color = sign, shape = race), size = 2) +
  facet_wrap(~person)
## Warning: Removed 10 rows containing missing values (geom_point).

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. # The x and y axis labels were interchanged. Please see corrected graph.

# load package
library(ggplot2)

p2 <- ggplot(mpg,aes(manufacturer,hwy))

# draw boxplot & change axis lines to vertical
p2 + geom_boxplot()+coord_flip()+labs(x="Vehicle Manufacturer", y="Highway Fuel Efficiency(miles/gallon) ")+theme_classic()

  1. Stacked Density Plot

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

library(ggthemes)
library(ggplot2)

diamonds_Data <- ggplot(diamonds, aes(x=price,color=cut,fill=cut))

# draw density plot
diamonds_Data <- diamonds_Data + geom_density(alpha=0.2) + labs(x="Diamond Price(USD)", y="Density", title="Diamond Price Density")

diamonds_Data+ theme_economist() + scale_colour_economist()

  1. Sideways bar plot

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

penguins_Data <- penguins
penguins_Data %>%
  count(sex, species) %>%
  ggplot() + 
  geom_col(aes(x = sex, y = n, fill = species)) +
  scale_fill_manual(values = c("darkorange", "purple", "cyan4")) + 
  facet_wrap(~species, ncol = 1) + 
  theme_minimal() + 
  theme(legend.position = 'none') + 
  labs(x = "Sex", y = "Count") + 
  coord_flip()

  1. Scatterplot

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

ggplot(data = penguins_Data, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) + 
  geom_point(aes(shape = species), size = 2) + 
  geom_smooth(method = 'lm', se = FALSE) +
  scale_color_manual(values = c("darkorange", "darkorchid", "cyan4")) + 
  labs(x = "Bill Length (mm)", y = "Bill Depth (mm)", color = "Species", shape = "Species")