# comment
# load library

library(tidyverse)

head(mtcars)
View(mtcars)

# our first chart
# one variable, continuous
ggplot(data = mtcars,
       mapping = aes(x = mpg)) +
  geom_histogram()

Untitled

ggplot(data = mtcars,
       mapping = aes(x = mpg)) +
  geom_density()

Untitled

ggplot(data = mtcars,
       mapping = aes(x = mpg)) +
  geom_boxplot()

Untitled

## base layer
base <- ggplot(data = mtcars,
       mapping = aes(x = mpg))

base + geom_histogram(bins=8, fill="#03ab89")

base + geom_freqpoly(bins=8)

Untitled

## one variable - discrete

student <- data.frame(id=1:5, 
                      gender=c("M","M","M","F","F"))

ggplot(data = student,
       mapping = aes(x = gender)) +
  geom_bar()

Untitled

## two variables, both continuous

base2 <- ggplot(mtcars, aes(x=hp, y=mpg))

base2 +
  geom_point(size=5, col="red", alpha=0.4) 

base2 +
  # linear model lm
  geom_smooth(method = "lm",
              col = "black",
              fill = "gold") +
  geom_point() +
  theme_minimal()

Untitled

Untitled

## two variables, one discrete, 
## one continuous

ggplot(diamonds,
       aes(price)) +
  geom_boxplot() +
  coord_flip()

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

ggplot(diamonds,
       aes(x=cut, y=price)) +
  geom_violin()

Untitled

Untitled

Untitled

## summarize data => build geom_col

p1 <- diamonds %>%
  group_by(cut) %>%
  summarise(
    med_price = median(price)
  ) %>%
  ggplot(aes(cut, med_price)) +
  geom_col() +
  theme_minimal()

Untitled

## qplot => quick plot
## ggplot2

p1 <- qplot(x=price, 
      data=diamonds, 
      geom="histogram",
      bins=10)