为了账号安全,请及时绑定邮箱和手机立即绑定

控制ggplot2图例显示顺序

控制ggplot2图例显示顺序

慕哥6287543 2020-02-01 15:42:17
有谁知道如何在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 shapeplot + labs(colour = "Clarity", shape = "Cut") # legend will be shape then colour
查看完整描述

2 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

在0.9.1中,确定图例顺序的规则是秘密且不可预测的。现在,在github中的dev版本0.9.2中,您可以使用参数设置图例的顺序。


这是示例:


plot <- ggplot(diamond.data, aes(carat, price, colour = clarity, shape = cut)) +

  geom_point() + opts(legend.position = "top")


plot + guides(colour = guide_legend(order = 1), 

              shape = guide_legend(order = 2))

在此处输入图片说明


plot + guides(colour = guide_legend(order = 2), 

              shape = guide_legend(order = 1))


查看完整回答
反对 回复 2020-02-01
  • 2 回答
  • 0 关注
  • 2567 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信