library(tidyverse)
library(DT)
library(scales)
library(ggthemes)
library(hrbrthemes)
library(plotly)
Graficacion
#Carga de paquetes
Carga de datos
<- read_csv("paises.csv") paises
# Tabla de datos de paises
|>
paises select(NAME, CONTINENT, LIFE_EXPECTANCY, GDP_PC) |>
datatable(
options = list(
pageLength = 5,
language = list(url = '//cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json')
) )
ggplot2
ggplot(data = paises) +
geom_point(mapping = aes(x = GDP_PC, y = LIFE_EXPECTANCY))
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer
#| label: grafico-02
#| warning: false
#| message: false
|>
paises ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY))+
geom_point() +
scale_x_continuous(labels = comma, limits = c(0, NA))
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer con colores por continente
#| label: grafico-03
#| warning: false
#| message: false
|>
paises ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY, color=CONTINENT))+
geom_point() +
scale_x_continuous(labels = comma, limits = c(0, NA))
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer con formas por continente
#| label: grafico-04
#| warning: false
#| message: false
|>
paises ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY, shape=CONTINENT))+
geom_point() +
scale_x_continuous(labels = comma, limits = c(0, NA))
Warning: The shape palette can deal with a maximum of 6 discrete values because more
than 6 becomes difficult to discriminate
ℹ you have requested 8 values. Consider specifying shapes manually if you need
that many have them.
Warning: Removed 26 rows containing missing values or values outside the scale range
(`geom_point()`).
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer con formas por continente
#| label: grafico-05
#| warning: false
#| message: false
|>
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("black", "blue", "green", "purple", "orange", "brown", "pink", "yellow"))
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer con formas por continente
#| label: grafico-06
#| warning: false
#| message: false
|>
paises ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY, size=POP_EST, 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("black", "blue", "green", "purple", "orange", "brown", "pink", "yellow"))
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer con formas por continente
#| label: grafico-07
#| warning: false
#| message: false
|>
paises ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY))+
geom_point() +
geom_smooth(method ="lm")+
scale_x_continuous(labels = comma, limits = c(0, NA))
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 11 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer con formas por continente
#| label: grafico-08
#| warning: false
#| message: false
|>
paises ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY))+
geom_point() +
geom_smooth(method ="loess")+
scale_x_continuous(labels = comma, limits = c(0, NA))
`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 11 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
PANELES
# Gráfico de dispersión de PIB per capita vs esperanza de vida al nacer con formas por continente
# paneles en función del continente
#| label: grafico-09
#| warning: false
#| message: false
|>
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))
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
# paneles en función de los ingresos y por continente
#| label: grafico-10
#| warning: false
#| message: false
|>
paises ggplot(aes(x = GDP_PC, y = LIFE_EXPECTANCY))+
geom_point() +
facet_grid(REGION_UN ~ INCOME_GRP)+
scale_x_continuous(labels = comma, limits = c(0, NA))
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
Títulos, etiquetas, estilos y colores
# Gráfico de dispersión de PIB per cápita vs esperanza de vida al nacer
# en África y Europa coloreado por continente
# + curva de tendencia
#| label: grafico-11
#| warning: false
#| message: false
|>
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")
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 7 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 7 rows containing missing values or values outside the scale range
(`geom_point()`).
Estilos (themes)
# Gráfico de dispersión de PIB per cápita vs esperanza de vida al nacer
# en África y Europa coloreado por continente
# + curva de tendencia
#| label: grafico-12
#| warning: false
#| message: false
|>
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_bw() # tema de ggplot2
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 7 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 7 rows containing missing values or values outside the scale range
(`geom_point()`).
# Gráfico de dispersión de PIB per cápita vs esperanza de vida al nacer
# en África y Europa coloreado por continente
# + curva de tendencia
#| label: grafico-13
#| warning: false
#| message: false
|>
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_ipsum() # tema de hrbrthemes
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 7 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning: Removed 7 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database
Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database
Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database
Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
# Gráfico de dispersión de PIB per cápita vs esperanza de vida al nacer
# coloreado por continente
#| label: grafico-14
#| warning: false
#| message: false
|>
paises 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(caption = "Fuentes: Natural Earth y Banco Mundial",
color = "Continente") +
labs(color = "Población estimada") +
scale_colour_brewer(palette = "YlOrBr", direction = -1) +
theme_ipsum() # estilo de hrbrthemes
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
Warning: Removed 11 rows containing non-finite outside the scale range
(`stat_smooth()`).
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: span too small. fewer data values than degrees of freedom.
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: pseudoinverse used at 10592
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: neighborhood radius 506.81
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: reciprocal condition number 0
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: There are other near singularities as well. 4.0698e+07
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : span too small. fewer
data values than degrees of freedom.
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
10592
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
506.81
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
number 0
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : There are other near
singularities as well. 4.0698e+07
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning in max(ids, na.rm = TRUE): ningun argumento finito para max; retornando
-Inf
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
font family not found in Windows font database
Plotly
# Gráfico de dispersión de PIB per cápita vs esperanza de vida al nacer
<-
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