worksheet-2.R 1.95 KB
Newer Older
Zheng Liu's avatar
Zheng Liu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
# Getting Started

library(readr)
person <- read_csv(
  file = 'data/census_pums/sample.csv',
  col_types = cols_only(
    ... = 'i',
    ... = 'd',
    ... = 'c',
    ... = 'c'))

## Layered Grammar

library(...)
...(person, ...(x = WAGP)) +
  ..._histogram()

library(dplyr)
person <- filter(
  person,
  WAGP > 0,
  WAGP < max(WAGP, na.rm = TRUE))

ggplot(person,
  aes(...)) +
  geom_point()

ggplot(person,
  aes(x = SCHL, y = WAGP)) +
  ...

## Layer Customization

ggplot(person,
  aes(x = SCHL, y = WAGP)) +
  geom_boxplot(...) +
  geom_point()

ggplot(person,
  aes(x = SCHL, y = WAGP)) +
  geom_boxplot() +
  geom_point(
    ... = 'red',
    ... = 'summary',
    fun.y = ...)

## Adding Aesthetics

ggplot(person,
  aes(x = SCHL, y = WAGP, ...)) +
  geom_boxplot()

person$SEX <- factor(person$SEX, levels = ...)

ggplot(person,
  aes(x = SCHL, y = WAGP, color = SEX)) +
  geom_boxplot()

# Storing and Re-plotting

... ggplot(person,
  aes(x = SCHL, y = WAGP, color = SEX)) +
  geom_point(
    stat = 'summary',
    fun.y = 'mean')

schl_wagp <- ... +
  scale_color_manual(
    values = c('black', 'red'))

ggsave(...,
  plot = ...,
  width = 4, height = 3)

# Smooth Lines

ggplot(person,
  aes(x = SEX, y = WAGP)) + 
  geom_point() +
  ...(
    method = ...,
    aes(group = 0))

# Axes, Labels and Themes

sex_wagp <- ggplot(person,
  aes(x = SEX, y = WAGP)) + 
  geom_point() +
  geom_smooth(
    method = 'lm',
    aes(group = 0))

sex_wagp + ...(
  ... = 'Wage Gap',
  x = ...,
  ... = 'Wages (Unadjusted USD)')

sex_wagp + ...(
  trans = 'log10')

sex_wagp + ...()

sex_wagp + theme_bw() +
  labs(title = 'Wage Gap') +
  theme(
    ... = element_text(
      face = 'bold',
      hjust = 0.5))

# Facets

person$SCHL <- factor(person$SCHL)
levels(person$SCHL) <- list(
  'High School' = '16',
  'Bachelor\'s' = '21',
  'Master\'s' = '22',
  'Doctorate' = '24')

ggplot(...,
  aes(x = SEX, y = WAGP)) + 
  geom_point() +
  geom_smooth(
    method = 'lm',
    aes(group = 0)) +
  ...