#Sections: Introduction, Prerequisites, First Steps, The mpg Data Frame, Creating a ggplot, A Graphing Template
#3.2.4 Exercises-1 # ggplot(data = mpg)
library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.1 v purrr 0.3.4
## v tibble 3.0.1 v dplyr 1.0.0
## v tidyr 1.1.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts -------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
mpg <- ggplot2::mpg
ggplot(data = mpg)

#3.2.4 Exercises-2 # rows & columns in mpg
dim(mpg)
## [1] 234 11
#3.2.4 Exercises-2 # rows in mpg
nrow(mpg)
## [1] 234
#3.2.4 Exercises-2 #columns in mpg
ncol(mpg)
## [1] 11
#3.2.4 Exercises-4 #scatterplot of hwy vs cyl
ggplot(data = mpg) + geom_point(mapping = aes(x = cyl, y = hwy))

#3.2.4 Exercises-5 #sscatterplot of class vs drv
ggplot(data = mpg) + geom_point(mapping = aes(x = drv, y = class))

#It depicts the combination of class and drv present in the dataset
#Sections: Aesthetic Mappings
#3.3.1 Exercises-1 #
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))

#3.3.1 Exercises-1 # manually changing color = "blue" should be placed outside the aes() argument
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")

#3.3.1 Exercises-2 #mpg
str(mpg)
## tibble [234 x 11] (S3: tbl_df/tbl/data.frame)
## $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
## $ model : chr [1:234] "a4" "a4" "a4" "a4" ...
## $ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
## $ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
## $ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
## $ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
## $ drv : chr [1:234] "f" "f" "f" "f" ...
## $ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
## $ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
## $ fl : chr [1:234] "p" "p" "p" "p" ...
## $ class : chr [1:234] "compact" "compact" "compact" "compact" ...
#3.3.1 Exercises-3# categorical vs. continuous variables
ggplot(data = mpg) +geom_point(mapping = aes(x = displ, y = hwy, color = displ))

#3.3.1 Exercises-3# categorical vs. continuous variables
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = displ))

#3.3.1 Exercises-4# same variable to multiple aesthetics
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = drv, shape = drv))

#3.3.1 Exercises-4#
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = displ, size = displ))

#3.3.1 Exercises-5# stroke
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), shape = 21, fill = 'red', size = 4, stroke = 3, color = 'white')

#3.3.1 Exercises-6#aes(colour = displ < 5)
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = displ < 5))

#Sections: Common Problems, Facets
#3.5.1 Exercises-1 #facet on a continuous variable
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ cty, nrow = 2)

#3.5.1 Exercises-1 #facet on a continuous variable
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(cty ~ year)

#3.5.1 Exercises-2 #facet_grid(drv ~ cyl)
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y= hwy)) + facet_grid(drv ~ cyl)

#3.5.1 Exercises-2 #facet_grid(drv ~ cyl)
ggplot(data = mpg) + geom_point(mapping = aes(x = drv, y = cyl))

#3.5.1 Exercises-3 #Code
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(drv ~ .)

#3.5.1 Exercises-3 #Code
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(. ~ cyl)

#3.5.1 Exercises-4 #first faceted plot
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ class, nrow = 2)

#3.6.1 Exercises-2 # code predictions
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-3 #show.legend = FALSE
ggplot(data = mpg) + geom_smooth( mapping = aes(x = displ, y = hwy, color = drv), show.legend = TRUE )
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-5 # two graphs look different
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot() + geom_point(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_smooth(data = mpg, mapping = aes(x = displ, y = hwy))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-6 # R COde
ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) + geom_point() + geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-6-2
ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) + geom_point() + geom_smooth(mapping = aes(group = drv), se = FALSE, show.legend = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-6-3
ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) + geom_point(mapping = aes(color = drv)) + geom_smooth(mapping = aes(color = drv, group = drv), se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-6-4
ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) + geom_point(mapping = aes(color = drv)) + geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-6-5
ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) + geom_point(mapping = aes(color = drv)) + geom_smooth(mapping = aes(linetype = drv), se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#3.6.1 Exercises-6-6
ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) + geom_point(mapping = aes(fill = drv), color = 'white', stroke = 2, shape = 21)

#3.6.1 Exercises-6-
ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) +
geom_point() +
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) +
geom_point() +
geom_smooth(mapping = aes(group = drv), se = FALSE, show.legend = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) +
geom_point(mapping = aes(color = drv)) +
geom_smooth(mapping = aes(color = drv, group = drv), se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) +
geom_point(mapping = aes(color = drv)) +
geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) +
geom_point(mapping = aes(color = drv)) +
geom_smooth(mapping = aes(linetype = drv), se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = mpg, mapping = aes(y = hwy, x = displ)) +
geom_point(mapping = aes(fill = drv), color = 'white', stroke = 2, shape = 21)

#Sections: Statistical Transformations
# 3.7.1 Exercises-1#stat_summary()
diamonds %>% group_by(cut) %>% summarize(median_y = median(depth),min_y = min(depth), max_y = max(depth)) %>%
ggplot() + geom_pointrange(mapping = aes(x = cut, y = median_y, ymin = min_y, ymax = max_y)) + labs(y = 'depth')
## `summarise()` ungrouping output (override with `.groups` argument)

# 3.7.1 Exercises-2
ggplot(data = diamonds) + geom_bar(aes(x = cut))

# 3.7.1 Exercises-2
diamonds %>% group_by(cut) %>% summarise(count = n()) %>% ggplot() + geom_col(mapping = aes(x = cut, y = count))
## `summarise()` ungrouping output (override with `.groups` argument)

# 3.7.1 Exercises-5 #group = 1
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop..))

# 3.7.1 Exercises-5
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

# 3.7.1 Exercises-5
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color, y = ..prop..))

# 3.7.1 Exercises-5
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color, y = ..prop.., group = color))

# 3.7.1 Exercises-5
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color, y = ..prop.., group = color), position = 'dodge')

#Sections: Position Adjustments
#3.8.1 Exercises-1 # R COde
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + geom_point()

#3.8.1 Exercises-1 # R COde- Problem with the code
# The problem with this plot is that since there are many observations with the same values of cty and hwy, many data points overlap with each other and it fails to show where the mass of the data is. We can use geom_jitter() instead:
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + geom_jitter()

#3.8.1 Exercises-3 # geom_jitter() with geom_count()
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + geom_count()

#3.8.1 Exercises-4 # geom_boxplot()
ggplot(data = mpg) +geom_boxplot(mapping = aes(y = displ, x = drv, color = factor(year)))

#Sections: Coordinate Systems
#3.9.1 Exercises-1 # pie chart using coord_polar()
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity)) + coord_polar()

#3.9.1 Exercises-4 #coord_fixed()
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) + geom_point() + geom_abline() + coord_fixed()
