library(tidyverse)
library(palmerpenguins)HDS 3.3-3.4
Begin by loading the tidyverse and palmerpenguins packages in the code chunk above and adding your name as the author.
Visualizing the penguins Data
Two Categorical Variables
Let’s start by making a stacked bar chart of the island variable by sex. Fill in the missing code:
# Insert your code here to make the stacked bar chart
ggplot(data = penguins, aes (
x = island,
fill = sex
))+
geom_bar() labs(
x = "Island",
y = "Number of Penguins",
fill = "Sex",
title = "Distribution of Penguin Species by Island and Sex"
)<ggplot2::labels> List of 4
$ x : chr "Island"
$ y : chr "Number of Penguins"
$ fill : chr "Sex"
$ title: chr "Distribution of Penguin Species by Island and Sex"
Modify the previous bar chart to make a side-by-side bar chart of the two variables:
ggplot(data = penguins, aes (
x = island,
fill = sex
))+
geom_bar( position = "dodge") labs(
x = "Island",
y = "Number of Penguins",
fill = "Sex",
title = "Distribution of Penguin Species by Island and Sex"
)<ggplot2::labels> List of 4
$ x : chr "Island"
$ y : chr "Number of Penguins"
$ fill : chr "Sex"
$ title: chr "Distribution of Penguin Species by Island and Sex"
One Quantitative/One Categorical Variables
Now let’s make side-by-side boxplots of body_mass_g by sex. Put the quantitative variable on the y-axis and the categorical variable on the x-axis.
ggplot(data = penguins, aes (
x = body_mass_g,
y = sex
))+
geom_boxplot()Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).
labs(
x = "Island",
y = "Number of Penguins",
title = "Distribution of Penguin Species by Island and Sex"
)<ggplot2::labels> List of 3
$ x : chr "Island"
$ y : chr "Number of Penguins"
$ title: chr "Distribution of Penguin Species by Island and Sex"
One Quantitative/Two Categorical Variables
Is there a difference in body_mass_g between sexes? Use facet_wrap() to modify the previous plot to compare the sexes separately for each species. Is the sex difference more or less clear now?
The plot makes the diffrence clearere
ggplot(data = penguins, aes (
x = body_mass_g,
y = sex
))+
geom_boxplot()+
facet_wrap(~sex)Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).
labs(
x = "Island",
y = "Number of Penguins",
title = "Distribution of Penguin Species by Island and Sex"
)<ggplot2::labels> List of 3
$ x : chr "Island"
$ y : chr "Number of Penguins"
$ title: chr "Distribution of Penguin Species by Island and Sex"
Two Quantitative Variables
Is there a relationship between flipper_length_mm and set the body_mass_g? Make a scatterplot of the two variables:
ggplot(data = penguins, aes (
x = flipper_length_mm,
y = body_mass_g
))+
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
labs(
x = "Flipper length (mm)",
y = "Body Mass (g)",
title = "Scatterplot between Penguin Flipper Length and Body Mass"
)<ggplot2::labels> List of 3
$ x : chr "Flipper length (mm)"
$ y : chr "Body Mass (g)"
$ title: chr "Scatterplot between Penguin Flipper Length and Body Mass"
Now add species to the plot by using different colored points:
ggplot(data = penguins, aes (
x = flipper_length_mm,
y = body_mass_g,
color = species
))+
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
labs(
x = "Flipper length (mm)",
y = "Body Mass (g)",
title = "Scatterplot between Penguin Flipper Length and Body Mass by Species"
)<ggplot2::labels> List of 3
$ x : chr "Flipper length (mm)"
$ y : chr "Body Mass (g)"
$ title: chr "Scatterplot between Penguin Flipper Length and Body Mass by Species"
Now make the size of the points proportional to bill_depth_mm.
ggplot(data = penguins, aes (
x = flipper_length_mm,
y = body_mass_g,
color = species,
size = bill_depth_mm
))+
geom_point()Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
labs(
x = "Flipper length (mm)",
y = "Body Mass (g)",
title = "Scatterplot between Penguin Flipper Length, Bill Depth and Body Mass by Species "
)<ggplot2::labels> List of 3
$ x : chr "Flipper length (mm)"
$ y : chr "Body Mass (g)"
$ title: chr "Scatterplot between Penguin Flipper Length, Bill Depth and Body Mass by Species "
What additional insight do you get from this?
This gives us more insight on how bill depth and flipper length can vary between spieces.