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
library(dplyr)
df_ejemplo <- gapminder %>%
select(country, year, lifeExp) %>%
filter(country == "Peru")
ggplot(data = df_ejemplo, mapping = aes(x = year, y = lifeExp)) +
geom_line()
gapminder %>%
select(country, year, lifeExp) %>%
filter(country == "Peru") %>%
ggplot(aes(year, lifeExp)) +
geom_line()
grafico <- gapminder %>%
select(country, year, lifeExp) %>%
filter(country == "Peru") %>%
ggplot(aes(year, lifeExp))
grafico +
geom_line()
grafico +
geom_col()
grafico +
geom_point()
(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
paises_por_continente %>%
ggplot(aes(continent, n)) +
geom_col()
gapminder %>%
filter(year == 2002) %>%
ggplot(aes(continent)) +
geom_bar()
gapminder %>%
filter(country == "Peru") %>%
ggplot(aes(year, gdpPercap)) +
geom_line()
gapminder %>%
filter(year == 2007) %>%
ggplot(aes(gdpPercap)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
gapminder %>%
filter(year == 2007) %>%
ggplot(aes(gdpPercap)) +
geom_boxplot()
https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf