Problema
Deseamos controlar el orden en el que se apilan las barras de un diagrama de barras en ggplot2.
- Gráfico original
Por defecto las barras se apilan alfabéticamente en función de la variable usada en el argumento fill.
library(reshape2)
library(ggplot2)
ra.melt <- melt(ra)
p <- ggplot(ra.melt, aes(x = variable, y = value))
p + geom_bar(aes(fill = quality), stat = "identity") +
labs(x = "group", y = "percentage (%)")
Comprobamos los niveles de la variable quality.
levels(ra.melt$quality)
[1] "A" "B" "C" "D" "E" "F"
[7] "G" "H" "I" "J" "K" "L"
[13] "M" "other"
ra <- structure(list(quality = structure(c(2L, 6L, 13L, 1L, 7L, 5L,
10L, 4L, 3L, 9L, 11L, 12L, 8L, 14L), .Label = c("A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "other"), class = "factor"), GY = c(25.5932389, 13.9567819, 11.7333, 8.6439, 7.8351008,
8.29, 6.059643, 3.8608517, 4.3277, 3.1710598, 1.2021144,
1.0986329, 0.9369271, 3.2907496), TH = c(23.0215577, 15.8975387,
10.5075, 7.8888, 8.8121504, 8.276, 6.4356378, 3.9147685,
5.4838, 3.9339386, 1.3921565, 0.742406, 0.8795013, 2.8142444
), SZ = c(21.217152, 16.0363831, 11.154, 8.5094, 7.945884,
10.2717, 5.4962929, 4.1381946, 5.1141, 3.4053352, 1.8472827,
1.0648934, 0.9792861, 2.820096), DZ = c(23.7548859, 14.4732136,
12.1292, 10.4722, 8.0838209, 7.0575, 5.144598, 4.174118,
5.4004, 3.1940577, 1.4427867, 1.1323864, 0.8986281, 2.6422047
), FP = c(19.9848602, 16.420735, 12.9683, 11.2059, 7.5143706,
6.8747, 5.8653262, 5.5412901, 4.182, 3.5347168, 1.3838249,
0.8811604, 0.8585596, 2.7842562)), .Names = c("quality",
"GY", "TH", "SZ", "DZ", "FP"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14"))
Solución
- En el orden original del data frame
ra.melt$quality <- factor(ra.melt$quality, levels = ra$quality)
p <- ggplot(ra.melt, aes(x = variable, y = value))
p + geom_bar(aes(fill = quality), stat = "identity") +
labs(x = "group", y = "percentage (%)")
factor(ra.melt$quality, levels = ra$quality)
[1] B F M A G E J D
[9] C I K L H other B F
[17] M A G E J D C I
[25] K L H other B F M A
[33] G E J D C I K L
[41] H other B F M A G E
[49] J D C I K L H other
[57] B F M A G E J D
[65] C I K L H other
Levels: B F M A G E J D C I K L H other
- En el orden reverso del original
ra.melt$quality <- factor(ra.melt$quality, levels = rev(ra$quality))
p <- ggplot(ra.melt, aes(x = variable, y = value))
p + geom_bar(aes(fill = quality), stat = "identity") +
labs(x = "group", y = "percentage (%)")
factor(ra.melt$quality, levels = rev(ra$quality))
[1] B F M A G E J D
[9] C I K L H other B F
[17] M A G E J D C I
[25] K L H other B F M A
[33] G E J D C I K L
[41] H other B F M A G E
[49] J D C I K L H other
[57] B F M A G E J D
[65] C I K L H other
Levels: other H L K I C D J E G A M F B
O alternativamente, usamos la función
fct_rev
library(forcats)
p <- ggplot(ra.melt, aes(x = variable, y = value))
p + geom_bar(aes(fill = fct_rev(quality)), stat = "identity") +
labs(x = "group", y = "percentage (%)")
- Si deseamos invertir el orden de las barras pero no de la leyenda, usamos el position_stack(reverse = TRUE)
p <- ggplot(ra.melt, aes(x = variable, y = value))
p + geom_bar(aes(fill = fct_rev(quality)), stat = "identity", position = position_stack(reverse = TRUE)) + labs(x = "group", y = "percentage (%)")
Referencias
Entradas relacionadas