请问有谁知道如何控制ggplot2中图例的顺序吗?
据我所见,图例的顺序似乎与实际比例标签相关,而不是比例声明顺序。更改比例标题会更改顺序。我使用钻石数据集制作了一个小例子来说明这一点。我想在一系列图中使用ggplot2,并希望其中一个变量始终出现在右侧。然而目前仅在其中一些图中发生这种情况,我不知道如何保持所需的顺序同时保留适当的比例标签。
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