---
title: "ggplot"
author: "alper yilmaz"
date: "8/3/2021"
output: html_document
tags: r-learn
---
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(cache=FALSE, warning=FALSE,
message=FALSE, echo=TRUE, dpi=180,
fig.width=8, fig.height=6)
```
# Ggplot
let's use gapminder package
```{r}
library(gapminder)
library(tidyverse)
```
```{r}
gapminder
```
## Layers
```{r}
gapminder %>%
ggplot()
```
```{r}
gapminder %>%
ggplot(aes(x=pop, y=gdpPercap))
```
```{r}
gapminder %>%
ggplot(aes(x=pop, y=gdpPercap)) +
geom_point()
```
```{r}
gapminder %>%
ggplot(aes(x=pop, y=gdpPercap)) +
geom_point(color="blue")
```
`aes()` is for mapping
```{r}
gapminder %>%
ggplot(aes(x=pop, y=gdpPercap)) +
geom_point(aes(color=continent))
```
```{r}
gapminder %>%
filter(year==2007) %>% # for latest year?
slice_max(n= 10, pop) %>% #arrange(-pop)
ggplot(aes(x=country, y=pop)) +
geom_col()
```
```{r}
gapminder %>%
filter(year==2007) %>% # for latest year?
slice_max(n= 10, pop) %>% #arrange(-pop)
ggplot(aes(x=country, y=pop)) +
geom_col() +
coord_flip()
```
```{r}
gapminder %>%
filter(year==2007) %>% # for latest year?
slice_max(n= 10, pop) %>% #arrange(-pop)
# mutate(country=fct_reorder(country,pop))
ggplot(aes(x=fct_reorder(country,pop), y=pop)) +
geom_col() +
coord_flip()
```
## about factors
```{r}
df <- tibble::tribble(
~color, ~a, ~b,
"blue", 1, 2,
"green", 6, 2,
"purple", 3, 3,
"red", 2, 3,
"yellow", 5, 1
)
df
```
```{r}
class(df$color)
```
```{r}
df$color <- factor(df$color)
class(df$color)
```
```{r}
df
```
```{r}
fct_reorder(df$color, df$a, min)
```
```{r}
p1 <- gapminder %>%
filter(year==2007) %>% # for latest year?
slice_max(n= 10, pop) %>% #arrange(-pop)
# mutate(country=fct_reorder(country,pop))
ggplot(aes(x=fct_reorder(country,pop), y=pop)) +
geom_col() +
coord_flip()
p1 + theme_classic()
```
```{r}
p1_lab <- p1 +
labs(title="Most population countries",
subtitle = "Year 2007",
x = "",
y = "Population",
caption = "Based on Gapminder data") +
theme_classic()
p1_lab
```
## second plot
```{r}
gapminder %>%
filter(year==2007) %>%
ggplot(aes(x=gdpPercap, y=lifeExp)) +
geom_point()
```
```{r}
p2 <- gapminder %>%
filter(year==2007) %>%
ggplot(aes(x=gdpPercap, y=lifeExp,color=continent, size=pop)) +
geom_point()
p2
```
```{r}
p2 +
labs(title="Life expantcy vs GDP per capita",
subtitle = "Rich nations live longer at year 2007",
x = "GDP per capita ($)",
y = "Life expactancy (years)",
caption = "Based on Gapminder data") +
theme_classic()
```
Let's try to match it with [Gapminder](https://www.gapminder.org/tools)
```{r}
p2_gap <- p2 +
scale_x_log10() +
coord_cartesian(ylim=c(0,90)) +
labs(title="Life expantcy vs GDP per capita",
subtitle = "Rich nations live longer at year 2007",
x = "GDP per capita ($)",
y = "Life expactancy (years)",
caption = "Based on Gapminder data") +
theme_classic()
p2_gap
```
## nice labels
```{r}
top_ten <- gapminder %>%
filter(year==2007) %>% # for latest year?
slice_max(n= 10, pop) %>%
mutate(country=as.character(country)) %>%
pull(country)
```
```{r}
gapminder %>%
filter(year==2007) %>%
mutate(label=if_else(country %in% top_ten, 1, 0))
```
```{r}
gapminder %>%
filter(year==2007) %>%
mutate(country=as.character(country)) %>%
mutate(label=if_else(country %in% top_ten, country, NA_character_))
```
```{r}
gapminder %>%
filter(year==2007) %>%
mutate(country=as.character(country)) %>%
mutate(label=if_else(country %in% top_ten, country, NA_character_)) %>%
ggplot(aes(x=gdpPercap, y=lifeExp,color=continent, size=pop)) +
geom_point() +
scale_x_log10() +
coord_cartesian(ylim=c(0,90)) +
labs(title="Life expantcy vs GDP per capita",
subtitle = "Rich nations live longer at year 2007",
x = "GDP per capita ($)",
y = "Life expactancy (years)",
caption = "Based on Gapminder data") +
geom_label(aes(label=label),color="black", size=4) +
theme_classic()
```
```{r}
p2_repel <- gapminder %>%
filter(year==2007) %>%
mutate(country=as.character(country)) %>%
mutate(label=if_else(country %in% top_ten, country, NA_character_)) %>%
ggplot(aes(x=gdpPercap, y=lifeExp,color=continent, size=pop)) +
geom_point() +
scale_x_log10() +
coord_cartesian(ylim=c(0,90)) +
labs(title="Life expantcy vs GDP per capita",
subtitle = "Rich nations live longer at year 2007",
x = "GDP per capita ($)",
y = "Life expactancy (years)",
caption = "Based on Gapminder data") +
ggrepel::geom_label_repel(aes(label=label),color="black", size=4,) +
theme_classic()
p2_repel
```
## combine multiple plots
```{r}
library(patchwork)
```
```{r}
p1_lab + p2_repel
```
```{r}
p1_lab / p2_repel
```