HDS_3-3_3-4.qdm

Author

Sandeep

library(tidyverse)
library(palmerpenguins)

Two Categorical Variables

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"
  )

Side-By-Side Bar Chart

ggplot(data = penguins, aes(x = island, fill = sex)) +
  geom_bar(position = "dodge") +
  labs(
    x = "Island",
    y = "Number of Penguins",
    fill = "Sex",
    title = "Penguins by Island and Sex (Side-by-Side)"
  )

One Quantitative/One Categorical Variables

ggplot(data = penguins, aes(x = sex, y = body_mass_g)) +
  geom_boxplot() +
  labs(
    x = "Sex",
    y = "Body Mass (g)",
    title = "Body Mass of Penguins by Sex"
  )
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).

One Quantitative/Two Categorical Variables

ggplot(data = penguins, aes(x = sex, y = body_mass_g)) +
  geom_boxplot() +
  facet_wrap(~ species) +
  labs(
    x = "Sex",
    y = "Body Mass (g)",
    title = "Body Mass of Penguins by Sex and Species"
  )
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).

Two Quantitative Variables

ggplot(data = penguins, 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()`).

Scatterplot by Species (Color)

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

Scatterplot with Point Size = Bill Depth

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