如何在不改变框的位置且避免长小数的情况下在以格:: bwplot制成的框中设置表示分数的x标签 [英] How to set x-labels in a box made in lattice::bwplot that represent fractions without altering position of boxes and avoiding long decimals

查看:73
本文介绍了如何在不改变框的位置且避免长小数的情况下在以格:: bwplot制成的框中设置表示分数的x标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是晶格新手,需要创建一个箱形图,其中y值表示电流.数据以1/24到8/1的不同比率进行整理.幸运地绘制时,我得到了盒子的相对位置,以尊重实际比例.我试图实现的是使用x标签(例如"1/24",1/12等)代替"0.04166667",0.08333333等.您可以看到下面的代码示例.我在面板函数内部尝试了不同的方法,还探讨了scales参数.对数据进行条件处理,并得到4个面板.棘手的是,右侧的面板和左侧的面板具有不同的x范围.

I am new to lattice and I need to create a box plot where y value represent electric current. The data is oranized on different ratios ranging from 1/24 to 8/1. When plotting fortunately I get the relative position of the boxes to respect the actual ratios. What I am trying to achieve is to use x-labels such as "1/24", 1/12 etc. instead of "0.04166667", 0.08333333 etc. You can see an example of the code bellow. I have tried different approaching inside the panel function and also explored the scales argument. The data is conditioned and 4 panels result. The tricky bit is that the panels from right and the panels from left have different x-ranges.

代码:

bwplot(current~factor(ac.ratio)|factor(constant.electrode)+factor(substrate), data=current.df, 
       scales=list(x=list(relation="free",
                          at=c(1/24, 1/12, 1/6, 1/3, 2/3, 1, 2, 4, 8),
                          labels = FALSE), 
                   y="same"), 
       layout=c(2,2), index.cond=list(2:1, 2:1))

我得到的一个例子是:

An example of what I obtain is:

我需要的是类似的东西:

And what I need is something similar to:

此外,左侧面板中的位置必须重叠(类似于右侧面板中的位置).这就是我所需要的(面板标题仅作为示例,而不是最终的).非常感谢.

Also the positions in the left panels must overlap (like those from the right panels). This is what I need (The panel titles are only as examples, not final). Thank you very much.

推荐答案

以下是使用 xyplot 代替 bwplot 并调用 panel.bwplot 内部.我必须手动调整 box.width 以获得合理的结果.

Here is a solution using xyplot in place of bwplot and calling panel.bwplot internally. I had to manually adjust box.width to get a reasonable result.

library(lattice)

dd <- data.frame(
  current = rnorm(200, 350, 50),
  ac.ratio = sample(c(1/24, 1/12, 1/6, 1/3), 200, replace = TRUE),
  substrate = gl(2, k = 100, labels = c("Acetate", "OECD"))
)

xyplot(current ~ ac.ratio | substrate, data = dd, box.width = 0.02,
       scales = list(x = list(at = c(1/24, 1/12, 1/6, 1/3),
                              labels = c("1/24", "1/12", "1/6", "1/3"))),
       panel = panel.bwplot, horizontal = FALSE)

这篇关于如何在不改变框的位置且避免长小数的情况下在以格:: bwplot制成的框中设置表示分数的x标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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