# load libraries

library(data.table)
library(ggplot2)

# clean work space

rm(list = ls())

# init colorscheme

COL <- c("#2271B2", "#E69F00", "#D55E00")
names(COL) <- c("blue", "orange", "red")
theme_set(
  theme_minimal(base_size = 13) +
    theme(
      panel.grid.minor = element_blank(),
      strip.text = element_text(face = "bold"),
      legend.position = "bottom"
    )
)
update_geom_defaults("point", list(size = 2))
update_geom_defaults("line", list(linewidth = 0.8))

d <- as.data.table(diamonds)

## 1.
ggplot(d[cut %in% c('Ideal', 'Premium')], aes(carat, fill=as.factor(cut))) +
  geom_histogram(alpha=0.75)

d[cut == 'Ideal', .(mean_hwy=mean(carat),
        range_hwy=diff(range(carat))),
    .(cut)]

## 2.
ggplot(d[clarity %in% c('SI2', 'SI1', 'VS1')], 
       aes(cut, fill=clarity)) +
  geom_bar(position=position_dodge())

d[, .N, .(cut, clarity)]

## 3.
ggplot(d[price < 10000], aes(cut, price, fill=clarity)) +
  geom_boxplot()

d[price < 1000, .(median_cty=as.numeric(median(price)),
        range_cty=diff(range(price))),
    .(cut, clarity)]

## 4.
ggplot(d[color == 'E'], aes(carat, price, colour=cut, fill=color)) +
  geom_point()