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.
# Load the data and filter for only hurricanes and tropical storms
library(nasaweather)
data("storms")

# Create a scatter plot with wind on the x-axis, pressure on the y-axis, and color by storm type
ggplot(storms, aes(x = wind, y = pressure, color = type)) +
  geom_point()

  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.
library(mdsr)
library(ggplot2)

data("MLB_teams")

# Plotting winning percentage against payroll, colored by division
ggplot(MLB_teams, aes(x = payroll, y = WPct, color = "red")) +
  geom_point() +
  scale_color_brewer(palette = "Set1") +
  labs(x = "Payroll (in millions)", y = "Winning Percentage",
       title = "Payroll and Winning Percentage by Division in MLB") +
  theme_minimal()

  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
#Load the RailTrail dataset.
data("RailTrail")
#Create a scatterplot of the number of crossings per day (volume) against the high temperature that day (high).
ggplot(data = RailTrail, aes(x = hightemp, y = volume)) +
  geom_point(color = "blue", size = 2) +
  labs(x = 'High temperature of the day', y = 'Number of crossings per day', 
       title = 'Daily Rail Train Crossings & Daily High Temperature') +
  facet_wrap(~weekday, nrow = 1) +
  geom_smooth(method = "lm", se = FALSE)

#Separate the plot into facets by weekday using the facet_wrap() function.
RailTrail$weekday <- replace(RailTrail$weekday, RailTrail$weekday == "FALSE", "Weekends / Holidays")
RailTrail$weekday <- replace(RailTrail$weekday, RailTrail$weekday == "TRUE", "Weekdays")

ggplot(data = RailTrail, aes(x = volume, y = hightemp)) +
  geom_point(color = "blue", size = 2) +
  labs(x = 'Number of crossings per day', y = 'High temperature of the day', 
       title = 'Daily Rail Train Crossings & Daily High Temperature') +
  facet_wrap(~weekday, ncol = 2)

#Add regression lines to the two facets using the geom_smooth() function.
ggplot(data = RailTrail, aes(x = volume, y = hightemp)) +
  geom_point(color = "blue", size = 2) +
  labs(x = 'Number of crossings per day', y = 'High temperature of the day', 
       title = 'Daily Rail Train Crossings & Daily High Temperature') +
  geom_smooth(method = "lm", se = TRUE) +
  facet_wrap(~weekday, ncol = 2)

  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.
library(nasaweather)
library(ggplot2)

ggplot(storms, aes(x=lat, y=long)) +
  geom_path(aes(color=name)) +
  facet_wrap(~year, ncol = 2) +
  ggtitle("Path of Tropical Storms between 1995 - 2000") + 
  xlab("Latitude") + 
  ylab("Longitude") +
  labs(color = "Storm Name")

  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.
#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?
library(palmerpenguins)
library(ggplot2)

ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "Bill Depth (mm)", y = "Bill Length (mm)", title = "Bill Length vs. Bill Depth by Species")

#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.
ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) +
  geom_point(aes(color = species)) +
  geom_smooth(method = "lm", se = FALSE) +
  labs(x = "Bill Depth (mm)", y = "Bill Length (mm)", title = "Bill Length vs. Bill Depth by Species") +
  facet_wrap(~species)