调整每个映射Legend 内部的顺序,如 cut 中 Fair,Good等的顺序,需要设置因子的水平,具体见:R语言学习 - 热图美化 (数值标准化和调整坐标轴顺序)
而如果有多个映射时,Legend 出现的顺序是不固定的。如果想固定或调整多个 legend的顺序,则可以通过guide_legend
函数逐个指定,如下面代码所示:
library(ggplot2)
p1 <- ggplot(diamonds, aes(carat, price, colour = clarity, shape = cut)) +
geom_point() + guides(shape = guide_legend(order = 1), colour = guide_legend(order = 2))
p2 <- ggplot(diamonds, aes(carat, price, colour = clarity, shape = cut)) +
geom_point() + guides(colour = guide_legend(order = 1), shape = guide_legend(order = 2))
library(patchwork)
p1+p2
结果图比较效果如下,仔细看 Legend 和代码中的1
和2
。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有