Does anyone know how I can get control of the ordering of legends in ggplot2?
From what I can see the order appears related to the actual scale labels rather than the scale declaration order. Changing the scale titles alters the ordering. I've made a small example using the diamond dataset to highlight this. I'm trying to use ggplot2 for a series of plots and I want to make one variable appear on the right in them all. At present though this only happens in some of them, and I'm at a loss on how to enforce my desired ordering whilst retaining the appropriate scale labels.
library(ggplot2)
diamond.data <- diamonds[sample(nrow(diamonds), 1000), ]
plot <- ggplot(diamond.data, aes(carat, price, colour = clarity, shape = cut)) +
geom_point() + opts(legend.position = "top", legend.box = "horizontal")
plot # the legend will appear shape then colour
plot + labs(colour = "A", shape = "B") # legend will be colour then shape
plot + labs(colour = "Clarity", shape = "Cut") # legend will be shape then colour
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…