ggplot2
basicsDuring 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.
A couple of tips, remember that there may be pre-processing involved in your graphics so you may have to do summaries or calculations to prepare, those should be included in your work.
To ensure accuracy pay close attention to axes and labels, you will be evaluated based on the accuracy and expository nature of your graphics. Make sure your axis labels are easy to understand and are comprised of full words with units if necessary.
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.
nasaweather package, create a
scatter plot between wind and pressure, with color being used to
distinguish the type of storm.library("nasaweather")
library("ggplot2")
head(storms)
## # A tibble: 6 × 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 D… 3
## 2 Allison 1995 6 3 6 18.3 -84.9 1004 30 Tropical D… 3
## 3 Allison 1995 6 3 12 19.3 -85.7 1003 35 Tropical S… 3
## 4 Allison 1995 6 3 18 20.6 -85.8 1001 40 Tropical S… 3
## 5 Allison 1995 6 4 0 22 -86 997 50 Tropical S… 4
## 6 Allison 1995 6 4 6 23.3 -86.3 995 60 Tropical S… 4
ggplot(storms, aes(x = pressure, y = wind, color = type)) +
geom_point(alpha = 0.6) +
labs(title = "Wind and Pressure as per Type of Storm",
x = "Pressure (mb)",
y = "Wind (knots)",
color = "Type") +
theme_mdsr()
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")
ggplot(MLB_teams, aes(x = payroll, y = WPct)) +
geom_point(size = 3, alpha = 0.6) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Relationship between winning percentage and payroll",
x = "Payroll (million dollars)",
y = "Winning Percentage") +
scale_x_continuous(labels = scales::dollar_format(scale = 1e-6)) +
theme_classic()
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.volume against the high temperature that dayweekday (an indicator
of weekend/holiday vs. weekday)library("mosaicData")
library("ggplot2")
data("RailTrail")
ggplot(RailTrail, aes(x = hightemp, y = volume)) +
geom_point(alpha = 0.6) +
labs(title = "Number of Crossings per Day by High Temperature",
x = "High Temperature (°F)",
y = "Number of Crossings per Day") +
theme_mdsr()
ggplot(RailTrail, aes(x = hightemp, y = volume)) +
geom_point(alpha = 0.6) +
facet_wrap(~ weekday, ncol = 1) +
labs(title = "Number of Crossings per Day by High Temperature and Weekday",
x = "High Temperature (°F)",
y = "Number of Crossings per Day") +
theme_classic()
ggplot(RailTrail, aes(x = hightemp, y = volume, color = weekday)) +
geom_point(alpha = 0.6) +
geom_smooth(method = "lm", se = FALSE) +
facet_wrap(~ weekday, ncol = 1) +
labs(title = "Number of Crossings per Day by High Temperature and Weekday",
x = "High Temperature (°F)",
y = "Number of Crossings per Day") +
theme_classic()
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")
data("storms")
storms_df <- as.data.frame(storms)
ggplot(storms_df, aes(x = long, y = lat, color = name)) +
geom_path() +
facet_wrap(~ year, ncol = 3) +
scale_color_discrete(name = "Storm Name") +
labs(title = "Paths of Tropical Storms",
x = "Longitude",
y = "Latitude") +
theme_classic()
penguins data set from the
palmerpenguins package.library("palmerpenguins")
library("ggplot2")
data("penguins")
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Relation between Bill Length and Bill Depth",
x = "Bill Length (mm)",
y = "Bill Depth (mm)",
color = "Species") +
theme_classic()
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(aes(color = species)) +
geom_smooth(method = "lm", se = FALSE) +
facet_wrap(~ species, ncol = 1) +
labs(title = "relation between Bill Length and Bill Depth",
x = "Bill Length (mm)",
y = "Bill Depth (mm)",
color = "Species") +
theme_mdsr()