Graficación

Author

Victor Quiros Vargas

Carga de paquetes

library(tidyverse)
library(DT)
library(scales)
library(ggthemes)
library(hrbrthemes)
library(plotly)

Carga de datos

paises <- read.csv("paises.csv")
# Tabla de datos de paises
paises |>
  select(NAME, CONTINENT, LIFE_EXPECTANCY, GDP_PC) |> 
  mutate(GDP_PC = round(GDP_PC, 2)) |> 
datatable(options = list(
  pageLength = 5,
  language = list(url = '//cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json'))
)

Ggplot2

paises |>
  ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY, shape = CONTINENT, color = CONTINENT)) +
  geom_point() +
  scale_x_continuous(labels = comma, limits = c(0, NA)) +
  scale_shape_manual(values = c(0, 1, 2, 3, 4, 5, 6, 7)) +
  scale_color_manual(values = c("red", "blue", "green", "purple", "orange", "brown", "pink", "yellow"))

Gráfico PIB PC y esperanza de vida al nacer

paises |>
  ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY)) +
  geom_point() +
  geom_smooth(method = 'lm') +
  scale_x_continuous(labels = comma, limits = c(0, NA))

paises |>
  ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY)) +
  geom_point() +
  facet_wrap(~ CONTINENT, nrow = 2) +
  scale_x_continuous(labels = comma, limits = c(0, NA))

paises |>
  filter(CONTINENT == 'Africa' | CONTINENT == 'Europe') |>
  ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY, color = CONTINENT)) +
  geom_point() +
  geom_smooth() +
  scale_x_continuous(labels = comma, limits = c(0, NA)) +
  scale_y_continuous(labels = comma, limits = c(50, 90)) +
  ggtitle("PIB per cápita vs esperanza de vida al nacer por continente") +
  xlab("PIB per cápita  (USD)") +
  ylab("Esperanza de vida (años)") +
  labs(subtitle = "Datos de África y Europa", 
       caption = "Fuentes: Natural Earth y Banco Mundial",
       color = "Continente") +
  theme_clean()

Plotly

grafico_ggplot2 <-
  paises |>
  ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY, color = CONTINENT)) +
  geom_point(aes(
    # datos que se muestran al colocar el ratón sobre un punto
    text = paste0(
      'País: ', NAME, "\n",
      "PIB per cápita: ", GDP_PC, "\n",
      "Esperanza de vida: ", LIFE_EXPECTANCY
    )
  )) +
  scale_x_continuous(labels = comma, limits = c(0, NA)) +
  scale_y_continuous(labels = comma, limits = c(50, 90)) +
  ggtitle("PIB per cápita vs esperanza de vida al nacer por continente") +
  xlab("PIB per cápita  (USD)") +
  ylab("Esperanza de vida (años)") +
  labs(caption = "Fuentes: Natural Earth y Banco Mundial",
       color = "Continente") +
  labs(color = "Población estimada") +
  theme_ipsum() # estilo de hrbrthemes

# Gráfico plotly
ggplotly(grafico_ggplot2, tooltip = "text") |> 
  config(locale = 'es') # para mostrar los controles en español

Gráfico de pastel

# Agrupar y resumir los datos
suma_poblacion_por_region <- paises |>
  group_by(REGION_UN) |>
  summarise(POP_TOTAL = sum(POP_EST))

# Calcular porcentajes
porcentaje_poblacion_por_region <- suma_poblacion_por_region |>
  mutate(POP_PCT = round(POP_TOTAL / sum(POP_TOTAL) * 100, 1))

# Gráfico de pastel
grafico_pastel_ggplot2 <-
  ggplot(porcentaje_poblacion_por_region, aes(x = "", y = POP_TOTAL, fill = REGION_UN)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y") +
  geom_text(
    aes(label = paste0(POP_PCT, "%")), 
    position = position_stack(vjust = 0.6) # para ajustar la posición del texto en cada porción
  ) +
  labs(title = "Distribución de la población por región de la ONU",
       x = NULL,
       y = NULL,
       fill = "Región de la ONU") +
  theme_void()

# Despliegue del gráfico
grafico_pastel_ggplot2