I think quality is stacked in the original order: B, F, M and so on. I suppose it is the order of the legend what you'd like to change:
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 (%)")
Or in reverse order:
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 (%)")
Notes
The legend takes the levels of the factor, which are sorted alphabetically by default:
levels(ra.melt$quality)
# Output
[1] "A" "B" "C" "D" "E" "F" "G"
"H" "I" "J" "K" "L" "M" "other"
With ra.melt$quality <- factor(ra.melt$quality, levels = ra$quality)
we set the order of the levels of the factor as they originally occur in the vector:
levels(ra.melt$quality)
#Output:
[1] "B" "F" "M" "A" "G" "E" "J"
"D" "C" "I" "K" "L" "H" "other"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…