---
title: "Data Visualization"
author: "Branko Arsić"
date: "05.03.2021."
output:
  html_document: default
  pdf_document: default
editor_options:
  chunk_output_type: console
---

Official book: http://www.cookbook-r.com/Graphs/ \
ggplot ekstenzije: https://exts.ggplot2.tidyverse.org/gallery/

If you’d like to learn more about the theoretical underpinnings of ggplot2 before you start, I’d recommend reading “The Layered Grammar of Graphics”, http://vita.had.co.nz/papers/layered-grammar.pdf.

## All layered grammar of graphics

ggplot(data = \<DATA>) + \
  \<GEOM_FUNCTION>(
     mapping = aes(\<MAPPINGS>), stat = \<STAT>, position = \<POSITION>) + \
  \<COORDINATE_FUNCTION> + \
  \<FACET_FUNCTION>


## Script setup

```{r setup, include=TRUE}
# install.packages("tidyverse", repos = "http://cran.us.r-project.org")
# https://www.tidyverse.org/packages/ -- zvanični sajt paketa
library(tidyverse)
```

## mpg dataset from ggplot2 

```{r mpg}
head(mpg)
summary(mpg)
```

## Including Plots
“The greatest value of a picture is when it forces us to notice what we never expected to see.” — John Tukey

ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

Osnovni grafik i prikaz po klasama:

```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class))
```

```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class))
```

```{r shapes, fig.cap="Oblici za tačke", out.width = "400px"}
knitr::include_graphics("shapes.png")
```

```{r}
mpg$class = factor(mpg$class)
ggplot(data=mpg) + geom_point(mapping = aes(x=displ, y=hwy, shape=class)) + scale_shape_manual(values=1:nlevels(mpg$class))
```

Bojenje:

```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
```

## Facets

```{r}
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_wrap(~ class, nrow = 2)
```

Kombinacija dve promenljive: 

```{r}
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_grid(drv ~ cyl)
```

## Geometric objects

```{r}
ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy))

# Umesto linetype mogli smo da koristimo 
# 1. group = drv
# 2. color = drv

ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv), show.legend = FALSE)
```

### multiple geoms in the same plot

```{r}
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) +
  geom_smooth(mapping = aes(x = displ, y = hwy))

# Kraći zapis:

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

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(color = class)) + 
  geom_smooth()

# Filtriranje podataka:

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(color = class)) + 
  geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)

```

### Domaći

```{r}
knitr::include_graphics("task1.png")
```

## Statistical transformations; diamond dataset

```{r}
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut))

# Ne radi bez group = 1
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
```

## Position adjustments

```{r}
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, colour = cut))

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = cut))

# stekovanje

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity))

# position adjustment: position = "identity", "dodge" or "fill".
# position = "identity" will place each object exactly where it falls in the context of the graph. This is not very useful for bars, because it overlaps them.

ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) + 
  geom_bar(alpha = 1/5, position = "identity")

ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) + 
  geom_bar(fill = NA, position = "identity")

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")

```

```{r}
# scatter plot again

# shows only 126 points of 234. This problem is known as overplotting.
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")

# Check geom_jitter()

```

## Coordinate systems

```{r}
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() + coord_flip()
```

<!--
http://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization
-->
