###Visualizing distributions
diamonds %>%
ggplot(aes(x = cut)) +
geom_bar()
diamonds %>%
ggplot(mapping = aes(x = carat)) +
geom_histogram(binwidth = 0.075)
diamonds %>%
filter(carat < 3) %>%
ggplot(aes(x = carat)) +
geom_histogram(binwith = .5)
## Warning in geom_histogram(binwith = 0.5): Ignoring unknown parameters:
## `binwith`
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
diamonds %>%
ggplot(aes(x = carat, color = cut)) +
geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
###Typical values
diamonds %>%
# filter out diamonds > 3 carats
filter(carat < 3) %>%
ggplot(aes(x = carat)) +
geom_histogram(binwidth = .01)
faithful %>%
ggplot(aes(eruptions)) +
geom_histogram(binwidth = .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`.
diamonds %>%
# filter(y < 3 | y > 20) %>%
mutate(y = ifelse(y < 3 | y > 20, NA, y)) %>%
ggplot(aes(x = x, y = y)) +
geom_point()
## Warning: Removed 9 rows containing missing values or values outside the scale range
## (`geom_point()`).
diamonds %>%
ggplot(aes(x = cut, y = price)) +
geom_boxplot()
diamonds %>%
count(color, cut) %>%
ggplot(aes(x = color, y = cut, fill = n)) +
geom_tile()
library(hexbin)
## Warning: package 'hexbin' was built under R version 4.4.3
diamonds %>%
ggplot(aes(x = carat,y = price)) +
geom_hex()
diamonds %>%
filter(carat < 3) %>%
ggplot(aes(x = carat,y = price)) +
geom_boxplot(aes(group = cut_width(carat, .1)))
library(modelr)
## Warning: package 'modelr' was built under R version 4.4.3
mod <- lm(log(price) ~ log(carat), data = diamonds)
diamonds_mod <- diamonds %>%
modelr::add_residuals(mod) %>%
mutate(resid = exp(resid))
diamonds_mod %>%
ggplot(aes(carat, resid)) +
geom_point()
diamonds_mod %>%
ggplot(aes(cut, resid)) +
geom_boxplot()