HDS 3.3-3.4

Author

Michael Ernst

library(tidyverse)
library(palmerpenguins)

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:

ggplot(data=penguins, mapping=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"
  )

Modify the previous bar chart to make a side-by-side bar chart of the two variables:

ggplot(data=penguins, mapping=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"
  )

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, mapping=aes(y=body_mass_g,x=sex))+
  geom_boxplot()+
  labs(
    x = "Sex",
    y = "Body Mass (in grams)",
    fill = "Sex",
    title = "Distribution of Penguin Body Mass by Sex"
  )
Ignoring unknown labels:
• fill : "Sex"
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).

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 sex difference seems more clear now that is consistent across species.

ggplot(data=penguins, mapping=aes(y=body_mass_g,x=sex))+
  geom_boxplot()+facet_wrap(~species)
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).

  labs(
    x = "Sex",
    y = "Body Mass (in grams)",
    title = "Distribution of Penguin Body Mass by Sex by Species"
  )
<ggplot2::labels> List of 3
 $ x    : chr "Sex"
 $ y    : chr "Body Mass (in grams)"
 $ title: chr "Distribution of Penguin Body Mass by Sex by Species"

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, mapping=aes(x=flipper_length_mm,y=body_mass_g))+
  geom_point()+
  labs(
    x="Flipper Length (mm)", y="Body Mass (g)",
    title="Body Mass vs. Flipper Length"
  )
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).

Flipper length and body mass appear correlated. Now add species to the plot by using different colored points:

ggplot(data=penguins, mapping=aes(x=flipper_length_mm,y=body_mass_g,color=sex))+
  geom_point()+
  labs(
    x="Flipper Length (mm)", y="Body Mass (g)",
    title="Body Mass vs. Flipper Length",
    color="Sex"
  )
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).

Now make the size of the points proportional to bill_depth_mm.

ggplot(data=penguins, mapping=aes(x=flipper_length_mm,y=body_mass_g,color=sex, size=bill_depth_mm))+
  geom_point()+
  labs(
    x="Flipper Length (mm)", y="Body Mass (g)",
    title="Body Mass vs. Flipper Length",
    color="Sex",
    size="Bill Depth (mm)"
  )
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).

What additional insight do you get from this? Bill depth appears negatively correlated to the other factors, perhaps seperation by species would tell me something further?