21

我偶然发现了 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])

标题长度?不,我现在将停止使用示例代码,但该代码也会产生与字符长度无关的波动订单。

有任何想法吗?

4

1 回答 1

18

正如我在上面的评论中提到的,没有办法控制和预测图例框的位置。我没有意识到这个问题。感谢您明确这一点。

可能有些人需要控制图例框,这里我快速修复一下:

# run this code before calling ggplot2 function
guides_merge <- function(gdefs) {
  gdefs <- lapply(gdefs, function(g) { g$hash <- paste(g$order, g$hash, sep = "z"); g})
  tapply(gdefs, sapply(gdefs, function(g)g$hash), function(gs)Reduce(guide_merge, gs))
}
environment(guides_merge) <- environment(ggplot)
assignInNamespace("guides_merge", guides_merge, pos = "package:ggplot2")

然后你可以使用order参数guide_legend(和guide_colorbar),

# specify the order of the legend.
qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl)+
 guides(size = guide_legend(order = 1), colour = guide_legend(order = 2), alpha = guide_legend(order = 3))

qplot(data = mpg,x = displ, y = cty, size = hwy, colour = class, alpha = cyl)+
 guides(size = guide_legend(order = 3), colour = guide_legend(order = 1), alpha = guide_legend(order = 2))

order参数应该是一个正整数。图例按顺序排列。请注意,这是一个快速修复,因此在 ggplot2 的下一个正式版本中可能会更改界面。

于 2012-04-06T00:38:37.427 回答