library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.4
## Warning: package 'readr' was built under R version 4.0.4
## Warning: package 'purrr' was built under R version 4.0.4
## Warning: package 'dplyr' was built under R version 4.0.4
## Warning: package 'stringr' was built under R version 4.0.4
## Warning: package 'forcats' was built under R version 4.0.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.4
gapminder <- read_xlsx("data/gapminder.xlsx")
gapminder
## # A tibble: 1,704 x 6
##    country     continent  year lifeExp      pop gdpPercap
##    <chr>       <chr>     <dbl>   <dbl>    <dbl>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## # ... with 1,694 more rows

ejemplo

library(dplyr)

df_ejemplo <- gapminder %>% 
  select(country, year, lifeExp) %>% 
  filter(country == "Peru")
ggplot(data = df_ejemplo, mapping = aes(x = year, y = lifeExp)) +
  geom_line()

Puedo usar pipes

gapminder %>% 
  select(country, year, lifeExp) %>% 
  filter(country == "Peru") %>% 
  ggplot(aes(year, lifeExp)) + 
  geom_line()

asignar mis gráficos a nombres

grafico <- gapminder %>% 
  select(country, year, lifeExp) %>% 
  filter(country == "Peru") %>% 
  ggplot(aes(year, lifeExp))

Ahora sólo haría falta agregarle una geometría

grafico +
    geom_line()

grafico +
    geom_col()

grafico +
    geom_point()

Grafico de barras o columnas

(paises_por_continente <- gapminder %>% 
  filter(year == 2002) %>% 
  group_by(continent) %>% 
  summarise(n = n()) %>% 
  ungroup() )
## # A tibble: 5 x 2
##   continent     n
##   <chr>     <int>
## 1 Africa       52
## 2 Americas     25
## 3 Asia         33
## 4 Europe       30
## 5 Oceania       2

dibujar nuestro primer gráfico de barras haciendo uso de geom_col()

paises_por_continente %>% 
  ggplot(aes(continent, n)) +
  geom_col()

usando geom_bar (solo frecuencias)

gapminder %>% 
  filter(year == 2002) %>% 
  ggplot(aes(continent)) +
  geom_bar()

Gráfico de líneas

gapminder %>% 
  filter(country == "Peru") %>% 
  ggplot(aes(year, gdpPercap)) +
  geom_line()

histograma

gapminder %>% 
  filter(year == 2007) %>% 
  ggplot(aes(gdpPercap)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Diagramas de caja o boxplot

gapminder %>% 
  filter(year == 2007) %>% 
  ggplot(aes(gdpPercap)) +
  geom_boxplot()

consulta

ggplot2: Elegant Graphics

https://ggplot2-book.org/index.html

R Graphics Cookbook, 2nd edition

https://r-graphics.org/