Code
library(openintro)
library(tidyverse)
library(ggplot2)
babies <- babies %>%
mutate(smoke = as.logical(smoke),
parity = as.logical(parity))library(openintro)
library(tidyverse)
library(ggplot2)
babies <- babies %>%
mutate(smoke = as.logical(smoke),
parity = as.logical(parity))Read documentation for this data set. Try ??babies
Task:
babies data,gestation to x-axis, bwt to y-axis, smoke to shape and color.ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point(size = 4) Warning: Removed 23 rows containing missing values or values outside the scale range
(`geom_point()`).
labs()Task:
babies data,gestation to x-axis, bwt to y-axis, smoke to shape.ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point(size = 4) +
labs(x = "Gestation (days)", #<<
y = "Birth Weight (ounces)", #<<
title = "Baby Gestation and Birth Weight by Mother's Smoker Status") #<<Warning: Removed 23 rows containing missing values or values outside the scale range
(`geom_point()`).
Let’s customize the theme. Try add a layer of theme_bw().
Other theme options:
theme_gray()theme_bw()theme_classic()theme_dark()theme_minimal()theme_void()ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point() +
labs(x = "Gestation (days)",
y = "Birth Weight (ounces)",
title = "Gestation and Birth Weight by Mother's Smoker Status") +
theme_bw() #<<ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point() +
labs(x = "Gestation (days)",
y = "Birth Weight (ounces)",
title = "Baby Gestation and Birth Weight by Mother's Smoker Status") +
theme_gray()ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point() +
labs(x = "Gestation (days)",
y = "Birth Weight (ounces)",
title = "Baby Gestation and Birth Weight by Mother's Smoker Status") +
theme_classic()ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point() +
labs(x = "Gestation (days)",
y = "Birth Weight (ounces)",
title = "Baby Gestation and Birth Weight by Mother's Smoker Status") +
theme_dark()ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point() +
labs(x = "Gestation (days)",
y = "Birth Weight (ounces)",
title = "Baby Gestation and Birth Weight by Mother's Smoker Status") +
theme_minimal()ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point() +
labs(x = "Gestation (days)",
y = "Birth Weight (ounces)",
title = "Baby Gestation and Birth Weight by Mother's Smoker Status") +
theme_void()theme()?themeThere are more aspects of a theme that we can control.
ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point() +
labs(x = "Gestation (days)",
y = "Birth Weight (ounces)",
title = "Baby Gestation and Birth Weight by Mother's Smoker Status") +
theme_bw() +
theme(title =
element_text(size = 12),
axis.title =
element_text(size = 10,
face="bold"))facet_grid() splits your plot into a grid of panels — one panel per group — so you can compare patterns across categories side by side. The formula rows ~ cols controls the direction: .~var places panels side by side in columns, while var~. stacks them in rows.
ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point(size = 4) +
facet_grid(.~parity) #<<Warning: Removed 23 rows containing missing values or values outside the scale range
(`geom_point()`).
ggplot(babies,
aes(x = gestation,
y = bwt,
shape = smoke,
color = smoke)) +
geom_point(size = 4) +
facet_grid(parity~.) #<<Warning: Removed 23 rows containing missing values or values outside the scale range
(`geom_point()`).
You can do much more in ggplot.
Resources:
Can you make this plot?
Hint: use filter() and is.na() to remove the NA values in the smoke column
babies %>%
filter(!is.na(smoke)) %>%
ggplot(
aes(x = smoke,
y = bwt)) +
geom_violin() +
labs(x = 'Mother\'s smoke history', y = 'Birth weight')+
theme_classic()Hint: use case_when() to create a new variable age_group and use geom_jitter() and geom_boxplot() to make this figure!
babies %>%
mutate(
age_grp = case_when(
age >= 30 ~ '30+',
20 <= age & age < 30 ~ '20-30',
age < 20 ~ 'under 20'
),
age_grp = factor(age_grp, levels = c('under 20','20-30','30+'))
) %>%
filter(!is.na(age_grp)) %>%
ggplot(aes(
x = age_grp,
y = bwt,
shape = age_grp,
color = age_grp)) +
geom_jitter(width = 0.2,alpha = 0.3) +
geom_boxplot(alpha = 0.5, size = 1) +
labs(x = 'Mother\'s age group', y = 'Birth weight')+
theme_classic()