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<-mosaicData::Marriage



ggplot(data=marriage_data,aes(y=delay,color=officialTitle,x=age))+
  geom_point(stat='identity')+
  scale_fill_brewer(palette='Dark2')+
  labs(y='Delay Between Ceremony and Court Date',x='Age of Person',color='Title of Official Conducting Marriage',title='Does the Age of the Person determine Delay?')

The visual cues used here are : 1. Position of the point tells us the delay between Ceremony Date and Court Date for an Individual 2. Position of the point tells us the Age for an Individual 3. The color tells us the type of official used for the wedding

ggplot(data=marriage_data,aes(prevcount))+
  geom_bar(aes(fill=prevconc,color=sign),position='dodge')+
  facet_wrap(race+officialTitle~.)+
  labs(y="No of Previous Marriages",x='Race of Person + Type of Official',fill='Why Previous Marriage Ended',color='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.

mpg_data<-mpg

mpg_data<-mpg_data%>%mutate(
  manufacturer<-factor(manufacturer)
)
ggplot(data=mpg_data,aes(manufacturer,hwy)) +
  geom_boxplot() +
  coord_flip() +
  theme_bw() +
  theme(
    panel.background = element_blank(),
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank())+
  labs(x='Vehicle Manufacturer',y= 'Highway Fuel Efficiency (miles/gallon)')

  1. Stacked Density Plot

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

diamonds_data<-ggplot2::diamonds
ggplot(data=diamonds_data,aes(x=price,group=cut,fill=cut))+
  geom_density(alpha=0.4) +
  scale_fill_brewer(palette = 'Pastel2') +
  theme_bw() +
  theme(
    panel.background = element_blank(),
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    )+
  labs(x='Diamond Price (USD)',y= 'Density',title='Diamond Price Density',fill='Cut')

  1. Sideways bar plot

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

penguins_data <-penguins

ggplot(data=penguins,aes(sex,color=species,fill=species))+
  geom_bar(stat='count') +
  facet_wrap(species ~., nrow=3) +
  coord_flip() +
  theme_minimal() +
  theme(
    panel.background = element_blank(),
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    legend.position = "none")

  1. Scatterplot

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

ggplot(data=penguins_data,aes(y=bill_depth_mm,x=bill_length_mm,color=species,shape=species)) +
  geom_point() +
  geom_smooth(se=FALSE, method='lm')+
  scale_color_brewer(palette = 'Dark2')+
  theme(
    panel.background = element_blank(),
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    legend.background=element_blank())+
  labs(y='Bill Depth(mm)',x='Bill Length(mm)',color='Species')+
  guides(shape=F)