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.
storms <- storms
ggplot(storms, aes(x = wind, y = pressure, color = type)) +
  theme_bw() + 
  geom_point() +
  labs(title = "Scatterplot of Wind vs Pressure", x = "Wind", y = "Pressure")

ggsave("scatter1.png")
knitr::include_graphics("scatter1.png")

  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.
MLB <- MLB_teams

ggplot(MLB, aes(x = WPct, y = payroll)) +
  theme_bw() + 
  geom_point() + 
  geom_smooth(method = 'lm', se = TRUE) +
  labs(title =  "Scatterplot of Winning Percentage vs Payroll", x = "Winning Percentage", y = "Payroll")

# ggsave("scatter2.png")
# knitr::include_graphics("scatter2.png")
  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
railtrail <- RailTrail
ggplot(railtrail, aes(x = volume, y = hightemp, color = weekday)) +
  theme_bw() + 
  geom_point() + 
  geom_smooth(method = 'lm', se = FALSE) +
  labs(title =  "Scatterplot of Volume vs High Temperature", x = "Volume", y = "High Temperature")

ggsave("scatter3.png")
knitr::include_graphics("scatter3.png")

  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.
Storms <- nasaweather::storms
Storms
## # A tibble: 2,747 × 11
##    name     year month   day  hour   lat  long pressure  wind type       seasday
##    <chr>   <int> <int> <int> <int> <dbl> <dbl>    <int> <int> <chr>        <int>
##  1 Allison  1995     6     3     0  17.4 -84.3     1005    30 Tropical …       3
##  2 Allison  1995     6     3     6  18.3 -84.9     1004    30 Tropical …       3
##  3 Allison  1995     6     3    12  19.3 -85.7     1003    35 Tropical …       3
##  4 Allison  1995     6     3    18  20.6 -85.8     1001    40 Tropical …       3
##  5 Allison  1995     6     4     0  22   -86        997    50 Tropical …       4
##  6 Allison  1995     6     4     6  23.3 -86.3      995    60 Tropical …       4
##  7 Allison  1995     6     4    12  24.7 -86.2      987    65 Hurricane        4
##  8 Allison  1995     6     4    18  26.2 -86.2      988    65 Hurricane        4
##  9 Allison  1995     6     5     0  27.6 -86.1      988    65 Hurricane        5
## 10 Allison  1995     6     5     6  28.5 -85.6      990    60 Tropical …       5
## # … with 2,737 more rows
ggplot(Storms, aes(x=lat, y=long))+
  geom_path(aes(col=name))+
  facet_wrap(~year)

ggsave("path1.png")
knitr::include_graphics("path1.png")

  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.

#a

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

ggsave("scatter4.png")
knitr::include_graphics("scatter4.png")

#b

ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
  geom_point() +
  geom_smooth(method = "lm", color = "purple") +
  facet_wrap(~species) +
  labs(
    col = 'Species',
    title = 'Scatterplot Between Bill Length and Depth By Species',
    x = 'Bill Length (mm)',
    y = 'Bill Depth (mm)',
  )

ggsave("scatter5.png")
knitr::include_graphics("scatter5.png")