Improving Data Visualization

1 Import data

Code
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

2 Scatter plot

Task:

  • Using the babies data,
  • Map gestation to x-axis, bwt to y-axis, smoke to shape and color.
  • Add a layer of points and set the size of the points to 4.
Code
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()`).

3 Add labels to axis using labs()

Task:

  • Using the babies data,
  • Map gestation to x-axis, bwt to y-axis, smoke to shape.
  • Add a layer of points and set the size of the points to 4.
  • Add labels to x-axis (Gestation), y-axis (Birth Weight), and the title of the plot (Palmer babies).
Code
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()`).

4 Themes

4.1 Theme options:

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()
Code
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() #<<

Code
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()

Code
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()

Code
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()

Code
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()

Code
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()

4.2 Explore theme()

?theme

There are more aspects of a theme that we can control.

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

5 Layout panels in a grid

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.

Code
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()`).

Code
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()`).

6 Summary

You can do much more in ggplot.

  • Use images
  • Make maps
  • Pick colors that you want,

Resources:

7 Practice

Can you make this plot?

7.1 Relationship between smoke mother v.s. birth weight

Hint: use filter() and is.na() to remove the NA values in the smoke column

Code
babies %>% 
  filter(!is.na(smoke)) %>% 
  ggplot(
       aes(x = smoke,
           y = bwt)) +
  geom_violin() +
  labs(x = 'Mother\'s smoke history', y = 'Birth weight')+
  theme_classic()

7.2 Relationship between mother’s age group v.s. birth weight

Hint: use case_when() to create a new variable age_group and use geom_jitter() and geom_boxplot() to make this figure!

Code
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()