## ----eval = FALSE--------------------------------------------------------
## install.packages("ggplot2")


## ------------------------------------------------------------------------
library(ggplot2)


## ------------------------------------------------------------------------
anscombe


## ------------------------------------------------------------------------
head(diamonds)
ggplot(data = diamonds)


## ------------------------------------------------------------------------
ggplot(data = diamonds, 
       mapping = aes(carat, price))


## ------------------------------------------------------------------------
ggplot(data = diamonds, mapping = aes(carat, price)) +
  geom_point()


## ------------------------------------------------------------------------
ggplot(data = diamonds, mapping = aes(carat, price)) +
  geom_point(aes(color = cut))


## ------------------------------------------------------------------------
ggplot(data = diamonds, 
       mapping = aes(carat, price)) +
  geom_point(aes(color = cut)) +
  geom_smooth(aes(color = cut), 
              method = "lm")


## ------------------------------------------------------------------------
ggplot(data = diamonds, mapping = aes(carat, price)) +
  geom_point(aes(color = cut)) +
  geom_smooth(aes(color = cut), method = "lm") +
  scale_y_sqrt()
  


## ------------------------------------------------------------------------
dim(mpg)
summary(mpg)
head(mpg)


## ------------------------------------------------------------------------
ggplot(data = mpg) +
  
  geom_point(mapping = aes(displ, hwy))


## ------------------------------------------------------------------------
ggplot(data = mpg) +
  geom_point(mapping = aes(displ, hwy, colour = class))


## ------------------------------------------------------------------------
ggplot(data = mpg) +
  geom_point(mapping = aes(displ, hwy), 
             colour = "darkgreen", size = 2)

## Your Turn --------------------------------------------------------------
# 1. Make a scatterplot of `cty` vs. `hwy` mpg using the `mpg` dataset.
# 2. Describe the relationship that you see.
# 3. Map color and shape to type of drive the car is (see `?mpg` for details on the variables.). Do you see any patterns?
# 4. Alter your plot fro part 3. to make all the points be larger.

ggplot(data = mpg) +
  geom_point(mapping = aes(x = cty, y = hwy))

## strong positive linear relationship

ggplot(data = mpg) +
  geom_point(mapping = aes(x = cty, y = hwy, colour = drv, shape = drv),
             size = 2)

## ------------------------------------------------------------------------
ggplot(data = mpg) +
  geom_histogram(mapping = aes(hwy), bins = 30) 

## histograms will look very different sometimes with different binwidths

## estimated density function
ggplot(data = mpg) +
  geom_density(mapping = aes(hwy)) 

ggplot(data = mpg) +
  geom_boxplot(mapping = aes("all cars", hwy)) 


ggplot(data = mpg) +
  geom_boxplot(mapping = aes(drv, hwy)) 

## boxplots allow us to see the distribution of a cts rv conditional on a discrete one
## we can also show the actual data at the same time
ggplot(data = mpg) +
  geom_boxplot(mapping = aes(drv, hwy)) +
  geom_jitter(mapping = aes(drv, hwy), alpha = .5)

ggplot(data = mpg) +
  geom_bar(mapping = aes(drv)) 
## shows us the distribution of a categorical variable


## ------------------------------------------------------------------------
ggplot(data = diamonds, mapping = aes(carat, price)) +
  geom_point(aes(color = cut))


## ------------------------------------------------------------------------
ggplot(data = diamonds, mapping = aes(carat, price)) +
  geom_point(aes(color = cut)) +
  facet_wrap(. ~ cut)


## ------------------------------------------------------------------------
ggplot(data = diamonds, mapping = aes(carat, price)) +
  geom_point(aes(color = cut)) +
  facet_grid(color ~ cut)

## Your Turn --------------------------------------------------------------
# 1. Make a histogram of `hwy`, faceted by `drv`.
# 2. Make a scatterplot that incorporates color, shape, size, and facets.
# 3. BONUS - Color your histograms from 1. by `cyl`. Did this do what you thought it would? (Look at `fill` and `group` as options instead).

ggplot(mpg, mapping = aes(hwy)) +
  geom_histogram(aes(fill = factor(cyl), group = factor(cyl)), bins = 15, position = "dodge") +
  facet_wrap(. ~ drv)

ggplot(mpg) +
  geom_jitter(aes(cyl, hwy, colour = year, shape = drv), height = 0)

ggplot(mpg) +
  geom_point(aes(year, hwy, colour = class, shape = class, size = displ)) +
  facet_wrap(~drv)
