I am creating some violin plots and want to colour them. It works for a matrix of dimension 9, as in my previous question
ggplot violin plot, specify different colours by group?
but when I increase the dimension to 15, the order of the colours are not respected. Why is this happening?
Here it works (9 columns):
library(ggplot2)
dat <- matrix(rnorm(250*9),ncol=9)
# Violin plots for columns
mat <- reshape2::melt(data.frame(dat), id.vars = NULL)
mat$variable_grouping <- as.character(sort(rep(1:9,250)))
pp <- ggplot(mat, aes(x = variable, y = value, fill = variable_grouping)) +
geom_violin(scale="width",adjust = 1,width = 0.5) + scale_fill_manual(values=rep(c("red","green","blue"),3))
pp
Here it does not work (15 columns):
library(ggplot2)
dat <- matrix(rnorm(250*15),ncol=15)
# Violin plots for columns
mat <- reshape2::melt(data.frame(dat), id.vars = NULL)
mat$variable_grouping <- as.character(sort(rep(1:15,250)))
pp <- ggplot(mat, aes(x = variable, y = value, fill = variable_grouping)) +
geom_violin(scale="width",adjust = 1,width = 0.5) + scale_fill_manual(values=rep(c("red","green","blue"),5))
pp
variable_grouping
variable is converted to a factor by ggplot and so uses the default level order (where1
comes before2
). If you want to control the order of the factor levels you'll need to set the levels yourself "by hand". I useforcats::fct_inorder()
for this because it's convenient, a lafill = forcats::fct_inorder(variable_grouping)
. – aosmith