向格子中的每个面板添加不同的唯一标签 [英] Add different unique labels to each panel in lattice

查看:71
本文介绍了向格子中的每个面板添加不同的唯一标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很清楚如何使用panel.text在格子中标记面板ltext参数.但是,如果我想为格子中的每个面板使用不同的唯一标签,该怎么办?让我用简化的Dotplot来说明我的观点:

It's quite clear How to label panels in lattice using panel.text or ltext arguments. However, what if I want to use a different, unique label for each panel in lattice? Let me illustrate my point with this simplified Dotplot:

library(Hmisc)
#example data
data <- data.frame(transport=rep(c("bicycle","bus"),each=2), 
                   att=rep(c("behaviour control","intention"),2),
                   value=c(4.134,4.5,3.77,2.4), Lo=c(3.92,4.37,3.51,2.2),
                   Hi=c(4.34,4.62,4.02,2.61))
#labels I want to use
labels.hi=c("likely","easy")
labels.lo=c("unlikely","difficult")
#example dotplot
png("test.png",width=300, height=400)
Dotplot(transport ~ Cbind(value, Lo, Hi) | att, data, col=1,
        panel = function(x, y,...) {
          panel.Dotplot(x, y,...)
          ltext(2.5,1.5, labels=labels.lo)
          ltext(4.5,1.5, labels=labels.hi)
        })
dev.off()

这段代码给了我下面的图:

This code gives me the plot below:

上部面板获得正确的标签(不太可能"和可能"),但下部面板仅获得上部面板标签的重复.相反,我想在下部面板中绘制其余标签(容易",困难"),但在与上部面板相同的位置.

The upper panel gets a correct labels ("unlikely" and "likely"), but the lower panel just gets duplicate of the upper panel labels. Instead, I want to plot the remaining labels ("easy", "difficult") in the lower panel, but in the same location as upper panel.

我知道我可以为每个标签使用ltext参数分别定义每个标签,但是考虑到我的真实"情节(heh)具有更多面板和更多不同的唯一标签,这是不切实际的解决方案.有什么建议?请只取格子.

I know I could define each label separately using ltext argument for every label, but it's quite impractical solution considering that my 'real-life' plot (heh) has more panels, and much more different unique labels. Any suggestions? Lattice only please.

推荐答案

(很高兴看到一个很好的晶格问题.)我不同意agsdydy认为下标将是一个很好的索引策略.在这种情况下,它们的作用是偶然的,因为您的积分与标签的编号相同且顺序相同.下标是为面板选择单个数据点的机制,而不是为面板建立索引的机制.考虑使用packet.number()panel.number()函数.在这种情况下,我相信他们会返回相同的值,但是如果您考虑到更复杂的条件因素,请查阅他们的共享帮助页面:

(Nice to see a good lattice question.) I don't agree with agstudy that subscripts would be a good indexing strategy. In this case they work by accident because your points are the same number as your labels and in the same order. Subscripts are that mechanism for picking individual data points for panels rather than a mechanism for indexing panels. Consider using the packet.number() or panel.number() functions. In this instance I believe they return the same values, but consult their shared help page if you have more complex conditioning factors in mind:

Dotplot(transport ~ Cbind(value, Lo, Hi) | att, data, col=1,
        panel = function(x, y,...) {
          panel.Dotplot(x, y,...)
          if(packet.number()==1){ ltext(c(2.5,4.5) ,1.5, labels= labels.lo)}
          if(packet.number()==2){ ltext(c(2.5,4.5) ,1.5, labels=labels.hi)}
        })

如果您将标签放在矩阵中,那么使用带有[[]的索引将很容易.

If you had your labels in a matrix it would have been easy to use indexing with "[".

lab.mat=matrix(c( labels.hi,labels.lo), 2)
lab.mat
#     [,1]     [,2]       
#[1,] "likely" "unlikely" 
#[2,] "easy"   "difficult"

png("test.png",width=300, height=400)
Dotplot(transport ~ Cbind(value, Lo, Hi) | att, data, col=1,
        panel = function(x, y,...) {
          panel.Dotplot(x, y,...)
          { ltext(c(2.5,4.5) ,1.5, labels= lab.mat[packet.number(),])}
        })
dev.off()

这篇关于向格子中的每个面板添加不同的唯一标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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