ggplot2The 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
(echo=TRUE).
mosaicData package.library(mosaicData)
head(Marriage)
## bookpageID appdate ceremonydate delay officialTitle person dob
## 1 B230p539 1996-10-29 1996-11-09 11 CIRCUIT JUDGE Groom 1964-04-11
## 2 B230p677 1996-11-12 1996-11-12 0 MARRIAGE OFFICIAL Groom 1964-08-06
## 3 B230p766 1996-11-19 1996-11-27 8 MARRIAGE OFFICIAL Groom 1962-02-20
## 4 B230p892 1996-12-02 1996-12-07 5 MINISTER Groom 1956-05-20
## 5 B230p994 1996-12-09 1996-12-14 5 MINISTER Groom 1966-12-14
## 6 B230p1209 1996-12-26 1996-12-26 0 MARRIAGE OFFICIAL Groom 1970-02-21
## age race prevcount prevconc hs college dayOfBirth sign
## 1 32.60274 White 0 <NA> 12 7 102 Aries
## 2 32.29041 White 1 Divorce 12 0 219 Leo
## 3 34.79178 Hispanic 1 Divorce 12 3 51 Pisces
## 4 40.57808 Black 1 Divorce 12 4 141 Gemini
## 5 30.02192 White 0 <NA> 12 0 348 Saggitarius
## 6 26.86301 White 1 <NA> 12 0 52 Pisces
# Load the necessary library
library(ggplot2)
# Create the plot
ggplot(data = Marriage, aes(x = prevconc, y = age, color = person)) +
geom_boxplot() +
labs(title = "Marriage Ages with Respect to Previous Marriage and Person Type", x = "Previous Marriage Condition", y = "Age")
Answers: - Create an informative and meaningful data graphic. The above
plot is able to effectively show the relationship between the age of
individuals with respect to their previous marriage condition and person
type. The plot shows the distribution of ages for each condition of the
previous marriage, with each boxplot color-coded by the type of
individual. This allows us to easily compare the age distributions
across different conditions of the previous marriage and person
types.
x-axis: This visual cue represents the prevconc variable and shows the condition of the previous marriage. Each boxplot represents a different condition of the previous marriage. y-axis: This visual cue represents the age variable and shows the age of the individual. The height of each boxplot shows the distribution of ages for individuals with a particular condition of the previous marriage. color: This visual cue represents the person variable and distinguishes between the type of individual (husband or wife) in the plot. The color of each boxplot corresponds to the type of individual.
ggplot(Marriage, aes(x = age, y = delay, color = officialTitle, shape = race)) +
geom_jitter(alpha = 0.5) +
facet_wrap(~ person) +
labs(title = "Marriage Delay vs Age by Official Title and Race",
x = "Age", y = "Delay", color = "Official Title", shape = "Race")
The above code uses the geom_jitter() function to create a scatterplot
with jittered points. The color aesthetic is used to encode the
officialTitle variable, and the shape aesthetic is used to encode the
race variable. The facet_wrap() function is used to create separate
panels for each value of the person variable. Finally, the labs()
function is used to add a title and axis labels to the plot.
Your objective for the next four questions will be write the code necessary to exactly recreate the provided graphics.
This boxplot was built using the mpg dataset. Notice the
changes in axis labels.
ggplot(data = mpg, aes(x = hwy, y = manufacturer)) +
geom_boxplot() +
labs(title = "Highway Fuel Efficiency by Manufacturer", x = "Highway MPG", y = "Manufacturer") +
coord_flip() +
theme_classic()
This graphic is built with the diamonds dataset in the
ggplot2 package.
ggplot(diamonds, aes(x=price, fill=cut)) +
geom_density(alpha=0.5, color="black") +
facet_wrap(~cut) +
labs(x="Diamond Price (USD)", y="Density", fill="Cut", title="Diamond Price Density by Cut") +
theme_minimal()
This graphic uses the penguins dataset and shows the
counts between males and females by species.
ggplot(penguins, aes(x = sex, fill = species)) +
geom_bar() +
facet_wrap(~species, ncol = 1) +
scale_fill_manual(values = c("red","green","yellow"), guide = FALSE) +
theme_minimal() +
coord_flip() +
labs(x = "Sex", y = "Count", fill = "Species")
## Warning: The `guide` argument in `scale_*()` cannot be `FALSE`. This was deprecated in
## ggplot2 3.3.4.
## ℹ Please use "none" instead.
This figure examines the relationship between bill length and depth
in the penguins dataset.
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point(shape = 21, size = 3, fill = 'white', stroke = 0.5) +
geom_smooth(method = 'lm', se = FALSE, color = 'black', size = 1) +
scale_color_manual(values = c("red", "green", "yellow")) +
labs(x = 'Bill Length (mm)', y = 'Bill Depth (mm)', color = 'Species') +
theme_classic()