R for Data Science

These are mostly exercise solutions as well as the examples that I found interesting.

1 Visualize

mpg |> 
  ggplot(aes(
    x = displ,
    y = hwy,
    color = class
  )) +
  geom_point()

mpg |> 
  ggplot(aes(
    x = hwy,
    y = displ,
  )) +
  geom_point(
    shape = 17,
    color = "pink",
    size = 5
  )

mpg |> 
  ggplot(aes(x = displ, y = hwy, colour = displ < 5)) +
  geom_point()

mpg |> 
  ggplot(aes(
    x = displ,
    y = hwy,
    color = drv
  )) +
  geom_point() +
  geom_smooth(aes(linetype = drv))
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(mpg, aes(
  x = displ,
  y = hwy
)) +
  geom_smooth()
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(mpg, aes(
  x = displ,
  y = hwy
)) +
  geom_point() +
  geom_point(
    data = mpg |> filter(class == "2seater"),
    color = "red"
  ) +
  geom_point(
    data = mpg |> filter(class == "2seater"),
    shape = "circle open", size = 3, color = "red"
    )

mpg |> 
  ggplot(aes(
    x = hwy
  )) +
  geom_density()

ggplot(mpg, aes(
  x = displ,
  y = hwy
)) +
  geom_point(size = 3) +
  geom_smooth(se = F)
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(mpg, aes(
  x = displ,
  y = hwy
)) +
  geom_smooth(aes(
    group = drv
  ), se = F) +
  geom_point()
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(mpg, aes(
  x = displ,
  y = hwy,
  color = drv
)) +
  geom_point() +
  geom_smooth(se = F)
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(mpg, aes(
  x = displ,
  y = hwy,
)) +
  geom_point(aes(color = drv)) +
  geom_smooth(se = F)
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(mpg, aes(
  x = displ,
  y = hwy,
)) +
  geom_point(aes(color = drv)) +
  geom_smooth(aes(linetype = drv), se = F)
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(mpg, aes(
  x = displ,
  y = hwy,
)) +
  geom_point(size = 4, color = "white") +
  geom_point(aes(color = drv))