Lucas Vandenberghe
2019-Apr-13 08:36 UTC
[R] How to display coloured group correlations with scale_colour_manual in ggpairs (R)?
I'm using ggpairs for data with 3 groups. The problem is that not all variables have all groups and therefore, some correlations only need to show 2 groups. Because of the automatic alphabetical ordering of the groups by ggpairs, the colouring is not consistent. The first colour is always assigned to the first factor level. (For example: group 1 = red, group 2 = blue, group 3 = green. But with variables having only the second and last group: group 2 = red and group 3 = blue.) I tried to solve this problem myself by adding a scale_colour_manual in the following way: scale_colour_manual(values = c("group1"="#F8766D", "group2"="#00BA38", "group3"="#619CFF")) This seems to work for the density plots on the diagonal (ggally_densityDiag) and for the scatter plots in the lower part (ggally_points), but for the correlations (ggally_cor) I only get the overal (black) correlations and none of the coloured group correlations anymore. While they were displayed before, but with wrong matching of colours and groups. Why are they not displayed anymore? Following code generates this plot<https://i.stack.imgur.com/Iw5s2.png>, the colours and groups are not matching. ggpairs(output.b[,c(13,17,18)], aes(colour = as.factor(output.b$country), alpha = 0.4), upper = list(continuous = function(data, mapping, ...) { ggally_cor(data = output.b, mapping = mapping) + scale_colour_manual(values = c("#F8766D", "#00BA38", "#619CFF"))}), lower = list(continuous = function(data, mapping, ...) { ggally_points(data = output.b, mapping = mapping) + scale_colour_manual(values = c("#F8766D", "#00BA38", "#619CFF"))}), diag = list(continuous = function(data, mapping, ...) { ggally_densityDiag(data = output.b, mapping = mapping) + scale_fill_manual(values = c("#F8766D", "#00BA38", "#619CFF"))})) The adapted code generated this plot<https://i.stack.imgur.com/DVrCt.png>, the coloured group correlations are not displayed anymore. ggpairs(output.b[,c(13,17,18)], aes(colour = as.factor(output.b$country), alpha = 0.4), upper = list(continuous = function(data, mapping, ...) { ggally_cor(data = output.b, mapping = mapping) + scale_colour_manual(values = c("group1"="#F8766D", "group2"="#00BA38", "group3"="#619CFF"))}), lower = list(continuous = function(data, mapping, ...) { ggally_points(data = output.b, mapping = mapping) + scale_colour_manual(values = c("group1"="#F8766D", "group2"="#00BA38", "group3"="#619CFF"))}), diag = list(continuous = function(data, mapping, ...) { ggally_densityDiag(data = output.b, mapping = mapping) + scale_fill_manual(values = c("group1"="#F8766D", "group2"="#00BA38", "group3"="#619CFF"))})) [[alternative HTML version deleted]]