# JUAN DIEGO BARRERO GONZALEZ
# Visualización de datos en R y con ggplot2
library(gapminder)
library(ggplot2)
library(DT)
library(hrbrthemes)
library(ggplotlyExtra)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(knitr)
str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
datatable(gapminder) # crea tabla interactiva para visualizar base de datos
## Gráficos paso a paso ##
ggplot(data=gapminder) # crear objeto ggplot

#### atributos estéticos ####
#Elegimos las variables gdpPercap, lifeExp y continent,
# donde la primera estará asignada a ‘x’, la segunda a ‘y’ y
# la última al atributo color
p <- ggplot(data = gapminder,mapping = aes(x = gdpPercap,y = lifeExp, color=continent))
p

#### Función Geom ####
# Queremos graficar un diagrama de dispersión o scatterplot, entonces elegimos
# la función geom_point y le pasamos como argumento size= 3, con esto le
# indicamos a ggplot el tamaño de los puntos que se aplicará al gráfico.
p + geom_point(size=3)

#### Sistemas de coordenadas y escalas ####
# se grafica con escala logarítmica para que los datos estén mejores distribuidos
p <- ggplot(data = gapminder,mapping = aes(x = gdpPercap,y = lifeExp,color = continent)) +
geom_point(size=3) +
coord_cartesian() +
scale_x_log10(labels= scales::dollar)
p

#### Paleta de colores ####
# utilizamos la función scale_color_brewer con Set2
pSet2 <- ggplot(data = gapminder,mapping = aes(x = gdpPercap,y = lifeExp,color= continent)) +
geom_point(size=3) +
scale_color_brewer(palette="Set2") +
coord_cartesian() +
scale_x_log10(labels= scales::dollar) +
labs(x = "Ingreso (GDP) Per Cápita", y = "Esperanza de vida",
title = "Crecimiento económico y esperanza de vida",
subtitle = "Los puntos se representan por año-país",
caption = "DataSource: Gapminder - Link: https://www.gapminder.org", color=" ") +
guides(color = guide_legend(override.aes = list(size = 5)))
pSet2

# utilizamos la función scale_color_brewer con Dark2
pdark2 <- ggplot(data = gapminder,mapping = aes(x = gdpPercap,y = lifeExp,color = continent)) +
geom_point(size=3)+
scale_color_brewer(palette="Dark2") +
coord_cartesian() +
scale_x_log10(labels= scales::dollar) +
labs(x = "Ingreso (GDP) Per Cápita", y = "Esperanza de vida",
title = "Crecimiento económico y esperanza de vida",
subtitle = "Los puntos se representan por año-país",
caption = "DataSource: Gapminder - Link: https://www.gapminder.org", color=" ") +
guides(color = guide_legend(override.aes = list(size = 5)))
pdark2

# Ahora utilizamos una escala manual de colores, donde especificamos los colores
# elegidos y las etiquetas de cada uno de ellos
pmanual <- ggplot(data = gapminder,mapping = aes(x = gdpPercap,y = lifeExp,color = continent)) +
geom_point(size=3) +
scale_color_manual(values = c("#41b6a6", "#f6e37c", "#f5a26b","#51b8df","#713580"), labels = c("Africa", "América","Asia", "Europa", "Oceanía")) +
coord_cartesian() +
scale_x_log10(labels= scales::dollar) +
labs(x = "Ingreso (GDP) Per Cápita", y = "Esperanza de vida",
title = "Crecimiento económico y esperanza de vida",
subtitle = "Los puntos se representan por año-país",
caption = "DataSource: Gapminder - Link: https://www.gapminder.org", color=" ") +
guides(color = guide_legend(override.aes = list(size = 5)))
pmanual

#### Temas ####
# Para cambiar la apariencia del gráfico podemos agregar otra capa con un tema
# en particular, existen temas que vienen ya incluidos en ggplot2
## Theme_gray
p_theme_bw <- ggplot(data = gapminder,mapping = aes(x = gdpPercap,y = lifeExp,color = continent)) +
geom_point(size=3)+
scale_color_manual(values = c("#41b6a6", "#f6e37c", "#f5a26b","#51b8df","#713580"), labels = c("Africa", "América","Asia", "Europa", "Oceanía")) +
coord_cartesian() +
scale_x_log10(labels= scales::dollar) +
labs(x = "Ingreso (GDP) Per Cápita", y = "Esperanza de vida",
title = "Crecimiento económico y esperanza de vida",
subtitle = "Los puntos se representan por año-país",
caption = "DataSource: Gapminder - Link: https://www.gapminder.org", color=" ") +
guides(color = guide_legend(override.aes = list(size = 5))) +
theme_grey()
p_theme_bw

## Theme_classic
ptheme_classic <- ggplot( data = gapminder,mapping = aes(x = gdpPercap,y = lifeExp,color = continent)) +
geom_point(size=3) +
scale_color_manual(values = c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels = c("Africa", "América","Asia", "Europa", "Oceanía")) +
coord_cartesian() +
scale_x_log10(labels= scales::dollar) +
labs(x = "Ingreso (GDP) Per Cápita", y = "Esperanza de vida",
title = "Crecimiento económico y esperanza de vida",
subtitle = "Los puntos se representan por año-país",
caption = "DataSource: Gapminder - Link: https://www.gapminder.org", color=" ") +
guides(color = guide_legend(override.aes = list(size = 5))) +
theme_classic()
ptheme_classic

#### customizacion de theme ####
# definiremos dos parámetros text_size y margin_size, que luego utilizaremos
# para modificar el tamaño del título, subtítulo y leyenda.
# Con legend.position= "bottom" cambiamos la posición de la leyenda
# correspondiente a continente, en este caso, le indicamos a R que la leyenda
# estará ubicada en la parte inferior del gráfico.
# Con legend.key= element_rect(fill= 'NA') eliminamos el recuadro gris de las leyendas.
# Con legend.text= element_text(color= "#2c204d", size= text_size)
# especificamos el color y el tamaño del texto de la leyenda.
# Con plot.title= element_text(size = text_size * 1.8,family= "Garamond",
# hjust = 0.5,vjust = 1,colour = "#2c204d", ...)
# especificamos el tamaño, fuente, ubicación y color del título.
# Con plot.subtitle= element_text(size = text_size * 1.3, family= "Garamond",
# hjust = 0.5,vjust = 1,colour = "#2c204d", ...)
# especificamos el tamaño, fuente, ubicación y color del título.
# Con plot.caption= element_text(size = 11,family = "Garamond", hjust = 1,
# vjust = 1, colour = "#2c204d",...)
# especificamos el tamaño, fuente, ubicación y color del título.
# Para no repetir los mismos valores de tamaño del texto en el theme,
# defino un parámetro: text_size y una variable: margin_size.
text_size <-16 # tamaño base del texto
margin_size <- text_size/2 #de los margenes
pTheme_customizado <- ggplot(data=gapminder, mapping=aes(x= gdpPercap, y= lifeExp, color= continent)) +
geom_point(size=3,alpha=0.6)+
scale_x_log10(labels= scales::dollar) +
scale_color_manual(values= c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels= c("Africa", "America", "Asia","Europa", "Oceanía")) +
labs(x="Ingreso (PBI) Per Capita", y="Esperanza de vida en años",
title ="Crecimiento económico y esperanza de vida",
subtitle ="Los puntos se representan por año-país",
caption = "DataSource: Gapminder- Link: https://www.gapminder.org.", color= '')+
guides(color = guide_legend(override.aes = list(size = 5))) +
theme_light() +
theme(legend.position="bottom", legend.key= element_rect(fill='NA'),
legend.text= element_text(color="#2c204d",
size= text_size),
legend.justification = "center",
plot.title = element_text(size=(text_size * 1.8),family ="Garamond",
hjust = 0.5,vjust = 1,
colour = "#2c204d", #
face = 'bold',
margin = margin(b = margin_size * 1.2)),
plot.subtitle = element_text(size = text_size * 1.3,
family ="Garamond", hjust = 0.5, vjust = 1,
colour = "#2c204d",
margin = margin(b = margin_size * 0.9)),
plot.caption = element_text(size = 11,family ="Garamond",
hjust = 1, vjust = 1,
colour = "#2c204d",
face='bold',
margin = margin(t = margin_size * 0.9)),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(color = "gray90", size = 0.5),
panel.grid.minor = element_line(color = "gray90", size = 0.25))
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
pTheme_customizado
## 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_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

#### ¿Cómo guardamos lo hecho hasta aquí? ####
# Utilizamos la sentencia ggsave ("nombre.extension", height= n, width= m,
# units= "in", type= ' '), como argumento le pasamos el nombre del gráfico
# con la extensión deseada (png, pdf, svg, entre otras), dimensiones del
# gráfico con height y width o con dpiy unidades con units en “in”,“cm” o “mm”).
ggsave("grafico_scatterplot1.png", height = 8, width = 10, units = "in", type= 'cairo')
#### Agregar una nueva capa ####
# El objetivo es agregar al gráfico, una curva suavizada correspondiente a una
# función de regresión, entonces vamos a utilizar la funcion geom_smooth.
# geom_smooth(method = "gam")
# geom_smooth(method = "lm")
# geom_smooth(method = "glm")
# geom_smooth(method = "loess")
text_size <-14 # tamaño base del texto
margin_size <- text_size/2 # para el margen
# Asignamos el gráfico `ggplot' al objeto p_capa_smooth
p_capa_smooth <- ggplot(data=gapminder, mapping=aes(x= gdpPercap, y= lifeExp)) +
geom_point(aes(color=continent),size=3,alpha=0.6) +
geom_smooth(method = 'gam', col = "#2c204d", size = 0.7,
fill = "gray60", alpha = 0.2) +
scale_x_log10(labels= scales::dollar) +
scale_color_manual(values= c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels= c("Africa", "America", "Asia","Europa", "Oceanía")) +
labs(x="Ingreso (PBI) Per Capita", y="Esperanza de vida en años",
title ="Crecimiento económico y esperanza de vida",
subtitle ="Los puntos se representan por año-país" ,
caption = "DataSource: Gapminder- Link: https://www.gapminder.org.",
color= '', fill="Población") +
guides(color = guide_legend(override.aes = list(size = 5))) +
theme_light() +
theme(legend.position="bottom", legend.key= element_rect(fill='NA'),
legend.text= element_text(color="#2c204d",
size= text_size),
legend.justification = "center",
plot.title = element_text(size=(text_size * 1.8),family ="Garamond",
hjust = 0.5,vjust = 1,
colour = "#2c204d", #
face = 'bold',
margin = margin(b = margin_size * 1.2)),
plot.subtitle = element_text(size = text_size * 1.3,
family ="Garamond", hjust = 0.5, vjust = 1,
colour = "#2c204d",
margin = margin(b = margin_size * 0.9)),
plot.caption = element_text(size = 11,family ="Garamond",
hjust = 1, vjust = 1,
colour = "#2c204d",
face='bold',
margin = margin(t = margin_size * 0.9)),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(color = "gray90", size = 0.5),
panel.grid.minor = element_line(color = "gray90", size = 0.25))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# llamamos al objeto p_capa_smooth
p_capa_smooth
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'
## 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.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

# Guardamos el gráfico con ggsave
ggsave("grafico_smooth.png", height = 8, width = 10, units = "in", type='cairo')
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'
#### Facetas ####
# Vamos a separar por columnas con facet_wrap teniendo en cuenta la variable continente
p_facet <- ggplot(data=gapminder, mapping=aes(x= gdpPercap, y= lifeExp, color=continent))+
geom_point(size=3,alpha=0.6)+
geom_smooth(method = 'gam', col = "#2c204d", size = 0.7,
fill = "gray60", alpha = 0.2)+
scale_x_log10(labels= scales::dollar)+
scale_color_manual(values= c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels= c("Africa", "America", "Asia","Europa", "Oceanía"))+
labs(x="Ingreso (PBI) Per Capita", y="Esperanza de vida en años",
title ="Crecimiento económico y esperanza de vida",
subtitle ="Los puntos se representan por año-país" ,
caption = "DataSource: Gapminder- Link: https://www.gapminder.org.", color= '')+
guides(color = guide_legend(override.aes = list(size = 5)))+
theme_bw()+
theme(legend.position="bottom")+
facet_wrap(~continent, ncol = 2, scales="free")
p_facet
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'

# Guardamos el gráfico
ggsave("grafico_facet.png", height = 8, width = 10, units = "in", type='cairo')
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'
#### Themes personalizados con el paquete hrbrthemes ####
# El paquete hrbrthemes ofrece varias opciones de temas para aplicar a un gráfico, algunas son:
# theme_ipsum()
# theme_ft_rc()
# theme_ipsum_ps()
# theme_ipsum_rc()
# theme_ipsum_tw()
# Entonces, en lugar de utilizar un theme customizado,
# eligiremos la última opción theme_ipsum_tw(), así obtendremos el siguiente gráfico:
p_facet_ipsum_tw <- ggplot(data=gapminder, mapping=aes(x= gdpPercap, y= lifeExp, color=continent))+
geom_point(size=3,alpha=0.6)+
scale_x_log10(labels= scales::dollar)+
scale_color_manual(values= c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels= c("Africa", "America", "Asia","Europa", "Oceanía"))+
labs(x="Ingreso (PBI) Per Capita", y="Esperanza de vida en años",
title ="Crecimiento económico y esperanza de vida",
subtitle ="Los puntos se representan por año-país" ,
caption = "DataSource: Gapminder- Link: https://www.gapminder.org.", color= '')+
guides(color = guide_legend(override.aes = list(size = 5)))+
theme(legend.position="bottom")+
facet_wrap(~continent, ncol = 2, scales="free")
p_facet_ipsum_tw

# Guardamos el gráfico
ggsave("grafico_facet_ipsum_tw.png", height = 8, width = 10, units = "in", type='cairo')
#### Gráficos combinados con el Paquete patchwork ####
text_size <-16 # tamaño base del texto
p1 <- ggplot(data=gapminder, mapping=aes(x= gdpPercap, y= lifeExp))+
geom_point(aes(color=continent),size=3,alpha=0.6)+
geom_smooth(method = 'gam', col = "#2c204d", size = 0.7,
fill = "gray60", alpha = 0.2)+
scale_x_log10(labels= scales::dollar)+
scale_color_manual(values= c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels= c("Africa", "America", "Asia","Europa", "Oceanía"))+
labs(x="Ingreso (PBI) Per Capita", y="Esperanza de vida en años", color= '')+
guides(color = guide_legend(override.aes = list(size = 5)))+
theme(legend.position="bottom", legend.key= element_rect(fill='NA'),
legend.text= element_text(color="#2c204d", size= text_size),
legend.justification = "center",
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(color = "gray90", size = 0.5),
panel.grid.minor = element_line(color = "gray90", size = 0.25))
p1
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'

p2 <-ggplot(data=gapminder, mapping=aes(x= gdpPercap, y= lifeExp, color=continent))+
geom_point(size=3,alpha=0.6)+
scale_x_log10(labels= scales::dollar)+
scale_color_manual(values= c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels= c("Africa", "America", "Asia","Europa", "Oceanía"))+
labs(x="", y="Esperanza de vida en años", color= '')+
guides(color = guide_legend(override.aes = list(size = 5)))+
theme(legend.position="none", axis.text.y = element_text(size = 12))+
# con legend.position= "none", elimino las leyenda correspondiente a la variable de continente
facet_wrap(~continent, ncol = 2, scales="free") #
p2

ggsave("grafico_p2.png", height = 8, width = 10, units = "in", type='cairo')
# Podemos elegir combinar los dos figuras así:
# p1 + p2, una figura a continuación de la otra
# p1 / p2, la primera figura en la parte superior y la segunda en la parte inferior.
# Con plot_annotation() agrego título, subtítulo y caption al gráfico
# combinado y con el theme defino el tamaño de las etiquetas.
#### Gráficos interactivos con plotly ####
# Utilizamos la librería plotly para convertir gráficos estáticos generados
# con ggplot2 en gráficos interactivos.
# Con la sentencia ggplotly(p_capa_smooth, tooltip = "text") convierto
# la figura anterior en una interactiva y con ‘tooltip = “text”’ le indicó que
# me muestre las variables con el formato especificado anteriormente.
p_facet_dark <- ggplot(data=gapminder, mapping=aes(x= gdpPercap, y= lifeExp, color=continent, text = paste('<b>Continente:</b> ' , continent,'<br />','<b>Ingreso per cápita:</b> ','$', round(gdpPercap,2),'<br>', '<b>Expectativa de vida:</b> ', round(lifeExp,1)))) +
geom_point(size=3,alpha=0.6)+
scale_x_log10(labels= scales::dollar)+
scale_color_manual(values= c("#41b6a6", "#f6e37c","#f5a26b","#51b8df","#713580"), labels= c("Africa", "America", "Asia","Europa", "Oceanía"))+
labs(x="Ingreso (PBI) Per Capita", y="Esperanza de vida en años",
title ="Crecimiento económico y esperanza de vida", subtitle ="Los puntos se representan por año-país" , caption = "DataSource: Gapminder- Link: https://www.gapminder.org.", color= '')+
guides(color = guide_legend(override.aes = list(size = 5)))+
theme_ft_rc()+
theme(legend.position="bottom")+
facet_wrap(~continent, ncol = 2, scales="free")
ggplotly(p_facet_dark, tooltip = "text")