Autor: Álvaro Alonso Fernández
Departamento de Ciencias de la Vida
Universidad de Alcalá (España)
Queremos realizar un gráfico de tipo ridgelines o también llamado joyplot. Se trata de un gráfico que representa la distribución de un valor númerico para varios niveles de un factor categórico. Sería similar a un histograma pero con la densidad de probabilidad. Es un gráfico muy útil para comparar visualmente la distribución de los datos de una variable dependiente entre varias categorias de una variable independiente.
Creamos nuestro dataframe:
#Datos a introducir:
a1<-sample(1:60,10)
a1
## [1] 41 27 13 18 17 42 57 4 50 60
a2<-sample(60:100,25)
a2
## [1] 60 63 97 72 69 85 92 84 83 67 78 74 71 68 99 66 88 82 75 94 95 80 89 79 76
#Creamos el dataframe
mi_datos2 <- data.frame(
"Valores" = 1:35,
"Ciudad" = c("Alicante", "Valencia", "Madrid", "Barcelona", "Soria", "Vitigudino","Valverde"),
"Numeros" = c(a1,a2))
mi_datos2
## Valores Ciudad Numeros
## 1 1 Alicante 41
## 2 2 Valencia 27
## 3 3 Madrid 13
## 4 4 Barcelona 18
## 5 5 Soria 17
## 6 6 Vitigudino 42
## 7 7 Valverde 57
## 8 8 Alicante 4
## 9 9 Valencia 50
## 10 10 Madrid 60
## 11 11 Barcelona 60
## 12 12 Soria 63
## 13 13 Vitigudino 97
## 14 14 Valverde 72
## 15 15 Alicante 69
## 16 16 Valencia 85
## 17 17 Madrid 92
## 18 18 Barcelona 84
## 19 19 Soria 83
## 20 20 Vitigudino 67
## 21 21 Valverde 78
## 22 22 Alicante 74
## 23 23 Valencia 71
## 24 24 Madrid 68
## 25 25 Barcelona 99
## 26 26 Soria 66
## 27 27 Vitigudino 88
## 28 28 Valverde 82
## 29 29 Alicante 75
## 30 30 Valencia 94
## 31 31 Madrid 95
## 32 32 Barcelona 80
## 33 33 Soria 89
## 34 34 Vitigudino 79
## 35 35 Valverde 76
#Lo ordenamso por el nombre de las ciudades:
mi_ordenado <- mi_datos2[order(mi_datos2$Ciudad), ]
mi_ordenado
## Valores Ciudad Numeros
## 1 1 Alicante 41
## 8 8 Alicante 4
## 15 15 Alicante 69
## 22 22 Alicante 74
## 29 29 Alicante 75
## 4 4 Barcelona 18
## 11 11 Barcelona 60
## 18 18 Barcelona 84
## 25 25 Barcelona 99
## 32 32 Barcelona 80
## 3 3 Madrid 13
## 10 10 Madrid 60
## 17 17 Madrid 92
## 24 24 Madrid 68
## 31 31 Madrid 95
## 5 5 Soria 17
## 12 12 Soria 63
## 19 19 Soria 83
## 26 26 Soria 66
## 33 33 Soria 89
## 2 2 Valencia 27
## 9 9 Valencia 50
## 16 16 Valencia 85
## 23 23 Valencia 71
## 30 30 Valencia 94
## 7 7 Valverde 57
## 14 14 Valverde 72
## 21 21 Valverde 78
## 28 28 Valverde 82
## 35 35 Valverde 76
## 6 6 Vitigudino 42
## 13 13 Vitigudino 97
## 20 20 Vitigudino 67
## 27 27 Vitigudino 88
## 34 34 Vitigudino 79
#Introducimos nueva variable:
numeros2<-sample(c(a1,a2))
mi_ordenado <-cbind(mi_ordenado,numeros2)
mi_ordenado
## Valores Ciudad Numeros numeros2
## 1 1 Alicante 41 68
## 8 8 Alicante 4 78
## 15 15 Alicante 69 17
## 22 22 Alicante 74 67
## 29 29 Alicante 75 88
## 4 4 Barcelona 18 72
## 11 11 Barcelona 60 71
## 18 18 Barcelona 84 74
## 25 25 Barcelona 99 13
## 32 32 Barcelona 80 94
## 3 3 Madrid 13 69
## 10 10 Madrid 60 76
## 17 17 Madrid 92 75
## 24 24 Madrid 68 18
## 31 31 Madrid 95 97
## 5 5 Soria 17 99
## 12 12 Soria 63 63
## 19 19 Soria 83 60
## 26 26 Soria 66 4
## 33 33 Soria 89 89
## 2 2 Valencia 27 57
## 9 9 Valencia 50 60
## 16 16 Valencia 85 82
## 23 23 Valencia 71 42
## 30 30 Valencia 94 85
## 7 7 Valverde 57 92
## 14 14 Valverde 72 66
## 21 21 Valverde 78 83
## 28 28 Valverde 82 50
## 35 35 Valverde 76 27
## 6 6 Vitigudino 42 41
## 13 13 Vitigudino 97 84
## 20 20 Vitigudino 67 95
## 27 27 Vitigudino 88 80
## 34 34 Vitigudino 79 79
Vamos a representar Ciudad frente a numeros2. Es decir, tenemos una variable categórica y una dependiente cuantitativa. Ahora cargamos la libreria de ggplo2 y ggridges:
library(ggplot2)
## Warning: replacing previous import 'lifecycle::last_warnings' by
## 'rlang::last_warnings' when loading 'tibble'
## Warning: replacing previous import 'lifecycle::last_warnings' by
## 'rlang::last_warnings' when loading 'pillar'
library(ggridges)
Vamos a hacer el gráfico más sencillo posible:
ggplot(mi_ordenado, aes(x = numeros2, y = Ciudad)) +
geom_density_ridges()
## Picking joint bandwidth of 7.8
Para ello se añade fill = "darkgreen", alpha = 0.25 en geom_density_ridges(). El número 0.25 nos indica la intensidad de transparencia:
ggplot(mi_ordenado, aes(x = numeros2, y = Ciudad)) +
geom_density_ridges(fill = "darkgreen", alpha = 0.25)
## Picking joint bandwidth of 7.8
Para ello se añade fill = Ciudad en aes():
ggplot(mi_ordenado, aes(x = numeros2, y = Ciudad, fill = Ciudad)) +
geom_density_ridges()+
geom_density_ridges(alpha=0.25)
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
Para ello se añade quantile_lines = TRUE, quantiles = c(0.05, 0.5, 0.95) en geom_density_ridges(). Con la última parte le indicamos el 5%, 50% y el 95%:
ggplot(mi_ordenado, aes(x = numeros2, y = Ciudad, fill = Ciudad)) +
geom_density_ridges()+
geom_density_ridges(quantile_lines = TRUE,
quantiles = c(0.05, 0.5, 0.95))
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
Eliminamos la leyenda, ya que tenemos los nombres de las ciudades en el eje y. Para ello utilizamos theme(legend.position = "none"):
ggplot(mi_ordenado, aes(x = numeros2, y = Ciudad, fill = Ciudad)) +
geom_density_ridges()+
geom_density_ridges(quantile_lines = TRUE,
quantiles = c(0.05, 0.5, 0.95))+
theme(legend.position = "none")
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
Pasamos nuestra figura a un objeto a para facilitar su posterior manejo:
a<-ggplot(mi_ordenado, aes(x = numeros2, y = Ciudad, fill = Ciudad)) +
geom_density_ridges()+
geom_density_ridges(quantile_lines = TRUE,
quantiles = c(0.05, 0.5, 0.95))+
theme(legend.position = "none")
Cargamos la libreria necesaria:
library(patchwork)
Y ahora mostramos los 6 fondos disponibles para la figura. Cada uno que elija el que más le guste. Tal vez el tema clásico permita ver mejor los gráficos:
grA<- a + theme_classic()+ theme(legend.position = "none")
grB<- a + theme_minimal()+ theme(legend.position = "none")
grC<- a + theme_light()+ theme(legend.position = "none")
grD<- a + theme_dark()+ theme(legend.position = "none")
grE<- a + theme_gray()+ theme(legend.position = "none")
grF<- a + theme_linedraw()+ theme(legend.position = "none")
(grA | grB ) / (grC | grD ) / (grE | grF )
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
Para ello añadimo scale_fill_brewer(palette="Reds") poniendo el nombre de la paleta. Los ejemplos aparecen más abajo.
grA<- a + scale_fill_brewer(palette="Reds") + ggtitle("Reds")
grB<- a + scale_fill_brewer(palette="Set1") + ggtitle("Set1")
grC<- a + scale_fill_brewer(palette="Spectral") + ggtitle("Spectral")
grD<- a + scale_fill_brewer(palette="Greys") + ggtitle("Greys")
grE<- a + scale_fill_brewer(palette="BuPu") + ggtitle("BuPu")
grF<- a + scale_fill_brewer(palette="PuBu") + ggtitle("PuBu")
grG<- a + scale_fill_brewer(palette="Set3") + ggtitle("Set3")
grH<- a + scale_fill_brewer(palette="Pastel2") + ggtitle("Pastel2")
grI<- a + scale_fill_brewer(palette="Dark2") + ggtitle("Dark2")
(grA | grB | grC ) / (grD | grE | grF ) / (grG | grH | grI )
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
## Picking joint bandwidth of 7.8
Hay numerosas paletas. Aquí un resumen:
Departamento de Ciencias de la Vida
Universidad de Alcalá (España)