# Paquetes necesarios
library(readxl)
library(dplyr)
library(ggplot2)
Obtención de los datos
gapminder <- read_excel("data/gapminder_subset.xlsx")
A continuación se da a conocer una muestra de 10 observaciones presentes en los datos.
gapminder %>%
sample_n(10)
| Panama |
Americas |
2007 |
75.537 |
3242173 |
9809.186 |
| Jamaica |
Americas |
1977 |
70.110 |
2156814 |
6650.196 |
| Costa Rica |
Americas |
1967 |
65.424 |
1588717 |
4161.728 |
| Panama |
Americas |
1997 |
73.738 |
2734531 |
7113.692 |
| New Zealand |
Oceania |
1987 |
74.320 |
3317166 |
19007.191 |
| El Salvador |
Americas |
1987 |
63.154 |
4842194 |
4140.442 |
| Puerto Rico |
Americas |
1977 |
73.440 |
3080828 |
9770.525 |
| Paraguay |
Americas |
1962 |
64.361 |
2009813 |
2148.027 |
| Peru |
Americas |
1957 |
46.263 |
9146100 |
4245.257 |
| Honduras |
Americas |
1972 |
53.884 |
2965146 |
2529.842 |
Por cuestiones de comodidad, cambiaremos el nombre de las columnas de nuestra data.
gapminder <- gapminder %>%
rename(pais = country,
continente = continent,
año = year,
expectativa_de_vida = lifeExp,
poblacion = pop,
pbi_per_capita = gdpPercap)
gapminder %>%
sample_n(10)
| Mexico |
Americas |
1957 |
55.190 |
35015548 |
4131.547 |
| Chile |
Americas |
1962 |
57.924 |
7961258 |
4519.094 |
| El Salvador |
Americas |
1977 |
56.696 |
4282586 |
5138.922 |
| Costa Rica |
Americas |
1982 |
73.450 |
2424367 |
5262.735 |
| Venezuela |
Americas |
1992 |
71.150 |
20265563 |
10733.926 |
| Mexico |
Americas |
1982 |
67.405 |
71640904 |
9611.148 |
| Uruguay |
Americas |
1977 |
69.481 |
2873520 |
6504.340 |
| Ecuador |
Americas |
1957 |
51.356 |
4058385 |
3780.547 |
| Chile |
Americas |
1982 |
70.565 |
11487112 |
5095.666 |
| Trinidad and Tobago |
Americas |
1957 |
61.800 |
764900 |
4100.393 |
Cantidad de países por continente
gapminder %>%
filter(año == "2007") %>%
count(continente)
Visto de manera gráfica:
gapminder %>%
filter(año == "2007") %>%
count(continente) %>%
ggplot(aes(x = continente, y = n)) +
geom_col()

Evolución del PBI per cápita peruano
Podemos obtener la tabla de datos de evolución del PBI peruano.
gapminder %>%
filter(pais == "Peru") %>%
select(pais, año, pbi_per_capita)
| Peru |
1952 |
3758.523 |
| Peru |
1957 |
4245.257 |
| Peru |
1962 |
4957.038 |
| Peru |
1967 |
5788.093 |
| Peru |
1972 |
5937.827 |
| Peru |
1977 |
6281.291 |
| Peru |
1982 |
6434.502 |
| Peru |
1987 |
6360.943 |
| Peru |
1992 |
4446.381 |
| Peru |
1997 |
5838.348 |
| Peru |
2002 |
5909.020 |
| Peru |
2007 |
7408.906 |
gapminder %>%
filter(pais == "Peru") %>%
ggplot(aes(x = año, y = pbi_per_capita)) +
geom_line(color = "orange")

Comparación entre países de alianza del pacífico
gapminder %>%
filter(pais %in% c("Peru", "Chile", "Colombia", "Mexico")) %>%
ggplot(aes(x = año, y = pbi_per_capita)) +
geom_line(aes(color = pais))

Comparación entre Perú y Colombia después de 1980
gapminder %>%
filter(pais %in% c("Peru", "Colombia"),
año > 1980) %>%
ggplot(aes(x = año, y = pbi_per_capita)) +
geom_line(aes(color = pais))

Cálculo del PBI total nacional
gapminder %>%
filter(pais == "Peru") %>%
mutate(pbi_total = pbi_per_capita * poblacion) %>%
select(pais, año, pbi_total)
| Peru |
1952 |
30164781548 |
| Peru |
1957 |
38827542286 |
| Peru |
1962 |
52130689938 |
| Peru |
1967 |
70222305898 |
| Peru |
1972 |
82860598386 |
| Peru |
1977 |
100438462619 |
| Peru |
1982 |
116626175121 |
| Peru |
1987 |
128465130363 |
| Peru |
1992 |
99734320550 |
| Peru |
1997 |
144488140094 |
| Peru |
2002 |
158181134667 |
| Peru |
2007 |
212448566598 |
Visto de manera gráfica:
gapminder %>%
filter(pais == "Peru") %>%
mutate(pbi_total = pbi_per_capita * poblacion) %>%
ggplot(aes(x = año, y = pbi_total)) +
geom_line(color = "orange")

Comparación de la evolución del PBI nacional entre Perú y Colombia
gapminder %>%
filter(pais %in% c("Peru", "Colombia")) %>%
mutate(pbi_total = pbi_per_capita * poblacion) %>%
ggplot(aes(x = año, y = pbi_total)) +
geom_line(aes(color = pais))

Top 10 de países con menor PBI per cápita en 2007
gapminder %>%
filter(año == 2007) %>%
select(pais, continente, pbi_per_capita) %>%
arrange(pbi_per_capita) %>%
mutate(rank = row_number()) %>%
filter(rank <= 10)
| Haiti |
Americas |
1201.637 |
1 |
| Nicaragua |
Americas |
2749.321 |
2 |
| Honduras |
Americas |
3548.331 |
3 |
| Bolivia |
Americas |
3822.137 |
4 |
| Paraguay |
Americas |
4172.838 |
5 |
| Guatemala |
Americas |
5186.050 |
6 |
| El Salvador |
Americas |
5728.354 |
7 |
| Dominican Republic |
Americas |
6025.375 |
8 |
| Ecuador |
Americas |
6873.262 |
9 |
| Colombia |
Americas |
7006.580 |
10 |
Top 10 de países con mayor PBI per cápita en 2007
gapminder %>%
filter(año == 2007) %>%
select(pais, continente, pbi_per_capita) %>%
arrange(desc(pbi_per_capita)) %>%
mutate(rank = row_number()) %>%
filter(rank <= 10)
| United States |
Americas |
42951.65 |
1 |
| Canada |
Americas |
36319.24 |
2 |
| Australia |
Oceania |
34435.37 |
3 |
| New Zealand |
Oceania |
25185.01 |
4 |
| Puerto Rico |
Americas |
19328.71 |
5 |
| Trinidad and Tobago |
Americas |
18008.51 |
6 |
| Chile |
Americas |
13171.64 |
7 |
| Argentina |
Americas |
12779.38 |
8 |
| Mexico |
Americas |
11977.57 |
9 |
| Venezuela |
Americas |
11415.81 |
10 |