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.#Load the data
data(storms)
#Convert type to a factor
storms$type <- factor(storms$type)
#Create a scatter plot
plot(storms$wind, storms$pressure,
col = storms$type, pch = 19,
xlab = "Wind", ylab = "Pressure",
main = "Wind vs. Pressure with Storm Type")
#Add a legend
legend("topright", legend = levels(storms$type),
col = 1:length(levels(storms$type)),
pch = 19, bty = "n")
MLB_teams data in the mdsr package
to create an informative data graphic that illustrates the relationship
between winning percentage and payroll in context.ggplot(MLB_teams, aes(x = log10(MLB_teams$payroll), y = WPct, color = lgID, size = W)) +
geom_point(alpha = 0.7, size=2) +
geom_smooth(method = "lm", color = "black", linetype = "dashed")+
scale_color_manual(values = c("red", "blue")) +
scale_size(range = c(3, 8)) +
labs(x = "Payroll (millions)", y = "Winning percentage", color = "League", size = "Wins") +
theme_minimal()
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)#Create a scatterplot of volume against high temperature, faceted by weekday
ggplot(RailTrail, aes(x = hightemp , y = volume)) +
geom_point() +
geom_smooth(method = "lm", color = "blue")+
facet_grid(. ~ dayType) +
labs(x = "High temperature", y = "Number of crossings per day", title = "Rail Trail Usage")
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.#Create a plot of the paths of tropical storms
ggplot(storms, aes(x = long, y = lat, group = name, color = name)) +
geom_path() +
labs(x = "Longitude", y = "Latitude", title = "Paths of Tropical Storms by Year") +
facet_wrap(~year, ncol = 3) +
scale_color_discrete(name = "Storm name")
penguins data set from the
palmerpenguins package.#Create a scatterplot of bill_length_mm against bill_depth_mm, colored by species with regression lines added
ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) +
geom_point() +
geom_smooth(method = "lm") +
labs(title = "Bill Length vs. Bill Depth by Species", x = "Bill Depth (mm)", y = "Bill Length (mm)") +
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
# Create a scatterplot of bill_length_mm against bill_depth_mm, faceted by species with regression lines added
ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm)) +
geom_point(aes(color = species)) +
facet_wrap(~ species, ncol = 3) +
geom_smooth(method = "lm") +
labs(title = "Bill Length vs. Bill Depth by Species", x = "Bill Depth (mm)", y = "Bill Length (mm)") +
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
#we can observe a positive association between bill depth and bill length, with the relationship being strongest for the Adelie penguins, followed by Gentoo and then Chinstrap.