ggplot2mosaicData package.Q1a) Create an informative and meaningful data graphic.
#The scatterplot shows the age and education level of different races who got married, with White race shown in blue, Hispanic race shown in green and Black race shown in orange. Each point represents a race, with the x-axis showing age and the y-axis showing education level.
library("mosaicData")
View(Marriage)
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
library(ggplot2)
ggplot(na.omit(Marriage), aes(x = age, y = college, color = race)) +
geom_point() +
labs(title = "Age and Education Level of Married Individuals",
x = "Age",
y = "Education Level",
color = "race") +
theme_bw()
Q1b) Identify each of the visual cues that you are using, and describe
how they are related to each variable. # X-axis: The x-axis represents
the age variable, and is a quantitative variable. The x-axis uses
position as the visual cue, with the values increasing from left to
right.
Y-axis: The y-axis represents the education variable, and is also a quantitative variable. The y-axis uses position as the visual cue, with the values increasing from bottom to top.
Color: The color aesthetic represents the race variable, which is a categorical variable. The color aesthetic uses hue as the visual cue, with White race shown in blue, Hispanic race shown in green and Black race shown in orange.
Q1c) Create a data graphic with at least five variables (either quantitative or categorical). For the purposes of this exercise, do not worry about making your visualization meaningful—just try to encode five variables into one plot.
ggplot(data = Marriage, aes(x = age, y = college)) +
geom_point(aes(color = sign, shape = race), size = 2) +
facet_wrap(~person)
## Warning: Removed 10 rows containing missing values (`geom_point()`).
Your objective for the next four questions will be write the code necessary to exactly recreate the provided graphics.
Q2. Boxplot Visualization
This boxplot was built using the mpg dataset. Notice the
changes in axis labels.
View(mpg)
boxplot_viz <- ggplot(mpg, aes(manufacturer, hwy))
boxplot_viz + geom_boxplot() + coord_flip() + labs(y = "Highway Fuel Efficiency (miles/gallon)", x = "Vehicle Manufacturer") + theme_classic()
This graphic is built with the diamonds dataset in the
ggplot2 package.
View(diamonds)
library("ggplot2")
stacked_density_plot <- ggplot(diamonds, aes(x = price, fill = cut, color = cut))
stacked_density_plot + geom_density(alpha = 1/5) + labs(x = "Diamond Price (USD)", y = "Density", title = "Diamond Price Density")
This graphic uses the penguins dataset and shows the
counts between males and females by species.
View(penguins)
ggplot(penguins, aes(y = sex, fill = species)) +
geom_bar() +
scale_fill_manual(values = c("darkorange", "purple", "cyan4")) +
facet_wrap(~ species, ncol = 1) +
theme_minimal() +
theme(legend.position = 'none') +
labs(x = "Count", y = "Sex")
This figure examines the relationship between bill length and depth
in the penguins dataset.
View(penguins)
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point(aes(shape = species), size = 2) +
geom_smooth(formula = 'y ~ x', method = 'lm', se = FALSE) +
scale_color_manual(values = c("darkorange", "darkorchid", "cyan4")) +
labs(color = 'Species', x = 'Bill Length (mm)', y = 'Bill Depth (mm)') +
guides(shape = FALSE)