title: “Week 7: Code Along 6” subtitle: “R For Data Science: Chapter 6 author:”Ethan Schena” date: “2025-03-06” output: html_document —

Introduction

Questions

Variation

Visualizing Distributions

diamonds %>%
    ggplot(aes(x = cut)) +
    geom_bar()

diamonds %>%
    ggplot(mapping = aes(x = carat)) +
    geom_histogram(binwidth = 0.5)

diamonds %>%
    filter(carat < 3) %>%
    ggplot(aes(x = carat)) +
    geom_histogram(binwidth = 0.5)

diamonds %>%
    ggplot(aes(x = carat, color = cut)) + 
    geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Typical Values

diamonds %>%
    
    # Filter Out Bigger Diamonds
    filter(carat > 3) %>%
    
    #Plot
    ggplot(aes(x = carat)) +
    geom_histogram(binwidth = 0.01)

faithful %>%
    
    ggplot(aes(eruptions)) +
    geom_histogram(binwidth = 0.25)

Unusual Values

diamonds %>%
    ggplot(aes(y)) +
    geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

diamonds %>%
    ggplot(aes(y)) +
    geom_histogram() + 
    coord_cartesian(ylim = c(0, 50))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Missing Values

diamonds %>%
    
    # filter(y < 3 | y > 20) %>%
    
    mutate(y_rev = ifelse(y < 3 | y > 20, NA, y)) %>%

# Plot
ggplot(aes(x = x, y = y)) +
    geom_point()

Covariation

Categorical and Continuous Variable

diamonds %>%
    
    ggplot(aes(x = cut, y = price)) +
    geom_boxplot()

Two Categorical Variables

diamonds %>%
    
    count(color, cut) %>%
    
    ggplot(aes(x = color, y = cut, fill = n)) +
    geom_tile()

Two Continuous Variables

library(hexbin)
diamonds %>%
    ggplot(aes(x = carat, y = price)) +
    geom_hex()

diamonds %>%
    ggplot(aes(x = carat, y = price)) +
    geom_boxplot(aes(group = cut_width(carat, 0.1)))