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.storm_data = storms %>%
select(wind, pressure, type) %>%
group_by(type)
ggplot(storm_data, aes(wind, pressure, color = type)) +
geom_point() +
labs(title = 'Scatter Plot Between Wind & Pressure Based on Storm Type',
col = 'Type', x = 'Wind', y = 'Pressure')
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 = payroll/1000000, y = WPct*100)) +
geom_point() +
geom_smooth(method = 'lm') +
labs(title = 'Relationship Between Payroll & Winning Percentage',
x = 'Payroll ($ in Millions)',
y = 'Winning Percentage (%)') +
theme(plot.title = element_text(hjust = 0.5))
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)#Part a
RailTrail %>%
ggplot(aes(x = volume , y = hightemp)) +
geom_point() +
geom_smooth(method = 'lm') +
labs(title = 'High Temperature per Day VS Number of Crossings',
x = 'Number of Crossings per Day',
y = 'Highest Temperature (Degree F)') +
theme(plot.title = element_text(hjust = 0.5))
#Parts b & c
RailTrail_data = RailTrail %>%
mutate(weekday_l = factor(weekday,
levels = c(TRUE, FALSE),
labels = c('Weekday', 'Weekend')
)
)
RailTrail_data %>%
ggplot(aes(x = volume, y = hightemp)) +
geom_point() +
geom_smooth(method = 'lm') +
facet_wrap(~weekday_l, ncol = 2) +
labs(title = 'High Temperature per Day VS Number of Crossings',
x = 'Number of Crossings per Day',
y = 'Highest Temperature (Degree F)') +
theme(plot.title = element_text(hjust = 0.5))
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.s_data = storms %>% filter(type == 'Tropical Storm')
s_data %>% ggplot(aes(x = lat, y = long)) +
geom_path(aes(color = name)) +
facet_wrap(~year, nrow = 3) +
labs(title = 'Path of Tropical Storms',
x = 'Longitude',
y = 'Latitude') +
theme(plot.title = element_text(hjust = 0.5))
penguins data set from the
palmerpenguins package.#Part a
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species, fill = species)) +
geom_point() +
geom_smooth(method = 'lm') +
labs(title = 'Bill Length VS Bill Depth (By Penguin Species)',
x = 'Bill Length (mm)',
y = 'Bill Depth (mm)') +
theme(plot.title = element_text(hjust = 0.5))
#Observation: Based on the plot, we can say that there is a direct relationship between the bill depth and the bill length, as bill depth increases with an increase in bill length. For all the three species, all the data points are closely spaced around the regression line. Among all the three species, 'Adelie' represents the highest variance.
#Part b
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species, fill = species)) +
geom_point() +
geom_smooth(method = 'lm') +
facet_wrap(~species) +
labs(title = 'Bill Length VS Bill Depth (By Penguin Species)',
x = 'Bill Length (mm)',
y = 'Bill Depth (mm)') +
theme(plot.title = element_text(hjust = 0.5))
#Observation: Based on the facet view, we can observe that the bill depth for 'Adelie' and 'Chinstrap' species are almost in the same range compared to the 'Gentoo' species. And when it comes to bill length, the 'Chinstrap' and 'Gentoo' species have their values in the range of 40-60mm, whereas the 'Adelie' species has its value in the range of 30-47mm.