我偶然发现了 ggplot2s 对图例进行排序的这种奇怪行为,只是无法弄清楚自动放置图例背后的逻辑是什么:
我的目标:在具有多个比例的情节中,我想以与自动顺序不同的(主题)顺序排列它们。但是我在 opts() 或 guides() 中找不到命令来为我执行此操作。并且要明确一点:我不想更改图例中的项目,这很好,但是多个完整图例的排列。
所以首先我假设它们是按类型排序的,即比例、颜色等。但事实并非如此,它们的位置发生了变化(见下文)。
按字母顺序?不。
library(ggplot2) ## v0.9
## Scale_colour on top
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
scale_size(name = "A") + scale_colour_discrete(name = "B")
## Reverse names --> scale_colour on bottom
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
scale_size(name = "B") + scale_colour_discrete(name = "A")
## Change name B to C --> scale_colour on bottom
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
scale_size(name = "C") + scale_colour_discrete(name = "A")
## Change name B to D --> scale_colour on top
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class) +
scale_size(name = "D") + scale_colour_discrete(name = "A")
scale_colour 的更多位置(用于交换的 scale_size 名称)
- “E”:底部
- “F” - “L”:顶部
- “M” - “N”:底部
它继续出现在顶部和底部。
阶乘顺序?不。
## From top to bottom: C - B - A
fname <- factor(c("A","B","C"), levels = c("A","B","C"))
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl) +
scale_size(name = fname[1]) + scale_colour_discrete(name = fname[2]) + scale_alpha(name=fname[3])
## From top to bottom: B - C - A
fname <- factor(c("A","B","C"), levels = c("C","B","A"))
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl) +
scale_size(name = fname[1]) + scale_colour_discrete(name = fname[2]) + scale_alpha(name=fname[3])
## From top to bottom: B - C - A
fname <- factor(c("A","B","C"), levels = c("B","C","A"))
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl)+
scale_size(name = fname[1]) + scale_colour_discrete(name = fname[2]) + scale_alpha(name=fname[3])
标题长度?不,我现在将停止使用示例代码,但该代码也会产生与字符长度无关的波动订单。
有任何想法吗?