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 preprocessing 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 hyperlinked and that I can see the visualization and the code required to create it.
nasaweather package, create a
scatterplot between wind and pressure, with color being used to
distinguish the type of storm.p1 <- ggplot(data = storms, aes(x = wind, y = pressure, color = type)) +
theme_bw() +
geom_point(size=0.5) +
labs(
title = 'Scatterplot: Wind speed and pressure by storm type',
x = 'Wind: \n maximum sustained wind speed (in knots)',
y = 'Pressure: \n at the storm’s center (in millibars)',
color = 'Type/Classification'
)
p1
MLB_teams data in the mdsr package
to create an informative data graphic that illustrates the relationship
between winning percentage and payroll in context.p2 <- ggplot(data = MLB_teams, aes(x = WPct, y = payroll / 1000000)) +
geom_point() +
geom_smooth(method = "lm") +
labs(title = 'Relationship between Payroll and Win% \nfor Major League Baseball teams from 2008-2014',
x= 'Win percentage (%)',
y = 'Payroll (in Millions)') +
theme_bw()
p2
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)p3 <- ggplot(data = RailTrail, aes(x = hightemp, y = volume,)) +
geom_point() +
geom_smooth(method = 'lm') +
theme_bw() +
labs(
title = 'Exploring relationship between trail crossings and temperature',
x = 'Daily high temperature (in degrees Farenheit)',
y = 'Volume: \nestimated number of trail users per day'
)
p3
RailTrail <-
RailTrail%>%
mutate(weekday = factor(weekday, labels = c('Weekend/Holiday', 'Weekday')))
p4 <- ggplot(data = RailTrail, aes(x = hightemp, y = volume,)) +
geom_point() +
geom_smooth(method = 'lm') +
facet_wrap(~ weekday) +
labs(
title = 'Exploring relationship between trail crossings and temperature during weekend/weekday',
x = 'Daily high temperature (in degrees Farenheit)',
y = 'Volume: \nestimated number of trail users per day'
)
p4
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.p5 <- ggplot(data = storms, aes(x = lat, y = long, color = name)) +
geom_path()+
facet_wrap(~ year) +
labs(
x = 'Latitude',
y = 'Longitute',
color = 'Storm Name',
title = 'Tropical storm path over the years')
p5
penguins data set from the
palmerpenguins package.p6 <- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) +
scale_color_brewer(palette = 'Dark2')+
geom_point() +
geom_smooth(method = 'lm') +
theme_bw() +
labs(
x = 'Bill depth (in mm)',
y = 'Bill length (in mm)',
color = 'Species',
title = 'Penguin bill length vs bill depth by species')
p6
p7 <- ggplot(penguins, aes(x = bill_depth_mm, y = bill_length_mm, color = species)) +
scale_color_brewer(palette = 'Dark2')+
geom_point() +
geom_smooth(method = 'lm') +
theme_bw() +
facet_wrap(~ species) +
labs(
x = 'Bill depth (in mm)',
y = 'Bill length (in mm)',
color = 'Species',
title = 'Penguin bill length vs bill depth by species')
p7
Across species, there appears to be a correlation between bill depth and bill length. The second graph with facets clearly shows the disparities between the species, which may be caused due to various reasons.