Directions

During ANLY 512 we will be studying the theory and practice of data visualization. We will be using R and the packages within R to assemble data and construct many different types of visualizations. We begin by studying some of the theoretical aspects of visualization. To do that we must appreciate the basic steps in the process of making a visualization.

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.

Questions

  1. Using data from the nasaweather package, create a scatter plot between wind and pressure, with color being used to distinguish the type of storm.
data1 <- nasaweather::storms

ggplot(data1, aes(wind, pressure, color = type)) +
  geom_point(size = 0.5) +
  labs(title = 'Scatter Plot (Wind * Pressure)',
     x = 'Wind', y = 'Pressure') 

  1. Use the MLB_teams data in the mdsr package to create an informative data graphic that illustrates the relationship between winning percentage and payroll in context.
data2 <- mdsr::MLB_teams

ggplot(data2, aes(WPct, payroll))+
  geom_point(size=0.5,color= "green")+
  labs(title = 'Relationship Between Winning Percentage and Payroll',
       x="Winning percentages of individual players",
       y= "Payroll of individual players") +
  geom_smooth(method = 'lm')

Q2 answer:
The plot above shows a positive slope of the best fitted line, which means there is positive relationship between winning percentage and payroll, in other words, the players who have a higher winning percentage get to be paid higher.

  1. The RailTrail data set from the mosaicData package describes the usage of a rail trail in Western Massachusetts. Use these data to answer the following questions.
  1. Create a scatterplot of the number of crossings per day volume against the high temperature that day
  2. Separate your plot into facets by weekday (an indicator of weekend/holiday vs. weekday)
  3. Add regression lines to the two facets
##a.
data3 <- mosaicData::RailTrail

ggplot(data3, aes(volume , hightemp)) +
  geom_point(size=0.5,color= "green") +
  geom_smooth(method = 'lm') + 
  labs(title = 'Relationship Between Temperature and Number of Crossings per day', 
       x = 'Number of Crossings per Day',
       y = 'High Temperature')

##b.c.
data3b <- data3 %>% 
  mutate(if_weekday = ifelse(weekday, "weekday", "weekend"))

  ggplot(data3b, aes(hightemp, volume)) +
    geom_point(size=0.5,color= "green") +
    geom_smooth(method = "lm") +
    facet_wrap(~ if_weekday, nrow = 1) +
    labs(title = 'Relationship Between Temperature and Number of Crossings by weekday',
         x = "High Temperature",
         y = "Number of Crossings per Day")

  1. Using data from the nasaweather package, use the geom_path function to plot the path of each tropical storm in the storms data table. Use color to distinguish the storms from one another, and use faceting to plot each year in its own panel.
data4 <- nasaweather::storms %>% filter(type == 'Tropical Storm')

ggplot(data4, aes(lat, long)) +
  geom_path(aes(color = name)) +
  facet_wrap(~year, nrow = 2) +
  labs(title = 'Path of Tropical Storms by years',
       x = 'Latitude',
       y = 'Longitude') 

  1. Using the penguins data set from the palmerpenguins package.
  1. Create a scatterplot of bill_length_mm against bill_depth_mm where individual species are colored and a regression line is added to each species. Add regression lines to all of your facets. What do you observe about the association of bill depth and bill length?
  2. Repeat the same scatterplot but now separate your plot into facets by species. How would you summarize the association between bill depth and bill length.
data5 <- palmerpenguins::penguins

ggplot(data5, aes(bill_length_mm, bill_depth_mm, color = species)) +
  geom_point() +
  geom_smooth(method = 'lm') +
  labs(title = 'Relationship of Bill Length and Bill Depth by penguin species',
       x = 'Bill Length',
       y = 'Bill Depth') 

#Q5a: 
#Based on the plot above, there is a positive relationship between the bill depth and the bill length, with an increase of bill depth there is an increase in bill length. 

ggplot(data5, aes(bill_length_mm, bill_depth_mm, color = species)) +
  geom_point() +
  geom_smooth(method = 'lm') +
  facet_wrap(~ species, nrow = 1) +
  labs(title = 'Relationship of Bill Length and Bill Depth by penguin species',
       x = 'Bill Length',
       y = 'Bill Depth') 

##Q5b:
#Based on the plot above, there are positive relationships between the bill depth and the bill length for all three penguin species. Also, the bill depth for species 'Adelie' and 'Chinstrap' are around 15-20 mm but  the 'Gentoo' species are lower than 17.5mm. For bill length, the 'Chinstrap' and 'Gentoo' species are around 40-60mm, but the 'Adelie' species' bill length is around 30-45mm.