订购多个图例/指南(什么是自动逻辑和如何更改它?) [英] Ordering of multiple legends/guides (what is the automatic logic & how to change it?)

查看:145
本文介绍了订购多个图例/指南(什么是自动逻辑和如何更改它?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了ggplot2s对传说排序的怪异行为,但无法弄清楚自动放置传说背后的逻辑是:



我的目标:在一个具有多个比例的情节中,我想按照与自动比例不同的(主题)顺序排列它们。但是我在opts()或guides()中找不到一个命令来为我执行此操作。而且要清楚的是:我不想改变传说中的项目,这很好,但安排了多个完整的传说。



因此,我首先假定他们是按类型排序的,例如比例尺,颜色等。但事实并非如此,他们的位置发生了变化(见下文)。

按字母顺序排列?

strong> No。

  library(ggplot2)## v0.9 

## Scale_colour on top
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class)+
scale_size(name =A)+ scale_colour_discrete(name =B)

##反向名称 - > scale_colour(bottom)
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class)+
scale_size(name =B)+ scale_colour_discrete(name =A )

##将名称B更改为C - > scale_colour底部
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class)+
scale_size(name =C)+ scale_colour_discrete(name =A )

##将名称B更改为D - > scale_colour上面
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class)+
scale_size(name =D)+ scale_colour_discrete(name =A )

scale_colour的其他位置(用于交换的scale_size名称)


  • E:bottom

  • F - L:top

  • M - N:底部


并且它会继续显示在底部顶部。

阶乘顺序?否。

  ##从上到下:C-B-A 
fname < - 因子(c(A,B,C),levels = c(A,B, C))
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class,alpha = cyl)+
scale_size(name = fname [1])+ scale_colour_discrete(name = fname [2])+ scale_alpha(name = fname [3])

##从上到下:B - C - A
fname < - factor(c (A,B,C),levels = c(C,B,A))
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class,alpha = cyl)+
scale_size(name = fname [1])+ scale_colour_discrete(name = fname [2])+ scale_alpha(name = fname [3])

##从上到下:B - C - A
fname < - factor(c(A,B,C),levels = c(B,C ,A))
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class,alpha = cyl)+
scale_size(name = fname [1] )+ scale_colour_discrete(name = fname [2])+ scale_alpha(name = fname [3])

标题的长度 $ b我现在将停止使用示例代码,但是这也会产生与字符长度无关的波动命令。



任何想法?

解决方案

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



也许有些人需要控制图例框,在这里我做了一个快速修正:
$ b $在调用ggplot2函数之前运行此代码
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 code> guide_legend (还有 guide_colorbar ),

 #指定图例的顺序。 
qplot(data = mpg,x = displ,y = cty,size = hwy,color = class,alpha = cyl)+
guides(size = guide_legend(order = 1),color = guide_legend order = 2),alpha = guide_legend(order = 3))

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

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


I stumbled onto this weird behavior with ggplot2s ordering of legends and just can't figure out what the logic behind the automatic placement of the legends is:

My aim: In a plot with multiple scales I want to arrange them in a different (thematic) order than the automatic one. But I couldn't find a command in opts() or guides() to do this for me. And just to be clear: I don't want to change the items within the legends, that works fine, but the arrangement of multiple complete legends.

So first I assumed they were ordered by type, i.e. scale, colour etc. But that is not the case, their position changes (see below).

Alphabetical order? No.

    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")

Further positions of scale_colour (for exchanged scale_size name)

  • "E": bottom
  • "F" - "L": top
  • "M" - "N": bottom

and it continues to appear on top an at the bottom.

Factorial order? No.

    ## 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])

Length of title? No. I'll stop for now with example code, but that one also yielded fluctuating orders independent of character length.

Any ideas?

解决方案

As I mentioned in the comment above, there is no way to control and predict the position of legend box. I wasn't aware of this problem. Thank you for making clear this.

Maybe some people need to control the legend box, here I put a quick fix:

# 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")

and then you can use order argument for guide_legend (and also 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 argument should be a positive integer. The legends are arranged along the order. Note that this is a quick fix so the interface may be changed in the next official version of ggplot2.

这篇关于订购多个图例/指南(什么是自动逻辑和如何更改它?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆