R 箱线图中自动调整标签
我们目前正在使用R
自动生成各种箱线图
。
我们遇到的问题是,标签的长度在不同的图和一个图中的类别之间变化很大。
有没有办法自动调整绘图,以便所有标签都能很好地适应它?
指定最坏情况 mar
是不可行的,因为在某些图中,标签比其他图中的标签短得多。
We are currently using R
to automatically generate various kinds of boxplots
.
The problem we have is that the length of our labels varies considerably between different plots and classes in one plot.
Is there a way to automatically adjust the plot so that all the labels will fit it nicely?
Specifying a worst case mar
isn't feasible because in some plots the labels are considerably shorter than in others.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Lattice 是最有可能在这里提供帮助的图形库。我这么说有两个原因:(i)点阵基于网格系统,通过访问网格的图形基元,您可以更好地控制面板输出的位置等。 (ii) 还有更多需要处理——R 标准图形包有 70 个不同的参数,而 Lattice 有 371 个——无论如何,根据我的计数,(length(names(unlist(trellis.par.get())))) ,然而这 371 个参数并不像基础包那样采用扁平结构,而是以分层结构收集(顶层有 30 个左右的参数组)。
您想要的是轴标签的相对定位。我建议针对此类任务降低一级。因此,要做你想做的事,只需更改相关的 grob 插槽,然后重新绘制两个 grob(使用 R 交互式提示):
“[.].xlab$”,grid.edit 接受一个 gPath 对象(只是遍历 gTree 的路径) ,这只是一个包含其他 grob 的 grob);因为我不知道我感兴趣的对象位于 gPath 中的哪个位置(x 轴/y 轴标签,我使用了该对象的正则表达式形式;
“grep=T”,只是告诉 grid.edit 处理前一个参数作为正则表达式;
“x=unit(0.5, 'npc')”,在此处指定视口坐标(在本例中,仅指定 x 值);'npc'('标准化父坐标',这是默认值)将视口原点视为 (0,0),并为其分配 1 个单位的宽度和高度,因此,我沿 x 轴指定了视口的中心。
Lattice is the graphics library most likely to be helpful here. I say that for two reasons: (i) lattice is based on the grid system, and by accessing grid's graphical primitives, you can get much finer control over, among other things, the location of your panel output; and (ii) there's more to work with--the R standard graphics package has 70 different parameters, while Lattice has 371--by my count anyway, (length(names(unlist(trellis.par.get())))), yet those 371 are not in a flat structure like they are in the base package, but instead are collected in a hierarchical structure (with 30 or so parameter groups at the top level).
What you want is relative positioning of your axis labels. I would recommend going down one level for this sort of task. So to do what you want, just change the relevant grob slots then just redraw the two grobs (using the R interactive prompt):
"[.].xlab$", grid.edit takes a gPath object (just a path traversing a gTree, which is just a grob that contains other grobs); because i didn't know where in the gPath my object of interest resides (the x-axis/y-axis label, i used a regular expression form for the object;
"grep=T", just tells grid.edit to treat the previous parameter as a regular expression;
"x=unit(0.5, 'npc')", specifying viewport coordinates here (in this case, just the x value); 'npc' ('normalized parent coordinates', which is the default) treats the viewport origin as (0,0), and assigns it a width & height of 1 unit each. Hence, i've specified the center of the viewport along the x axis.
使用基本绘图系统,快速解决方案可能是使用
las=2
或las=3
将 x 标签旋转为垂直。当然,这也仅在您的标签不是非常长的情况下才有效,但是超过一定的标签长度,您无论如何都会遇到任何类型的绘图问题(缩短标签将是当时的方法)。但我同意@doug的观点,为了更细粒度的控制,应该考虑lattice或ggplot2。
With the base plotting system, a quick solution could be to rotate the x-labels to be vertical, using
las=2
orlas=3
. Of course this also only works if your labels are not extremely long, but beyond a certain label length you will run into trouble with any type of plot anyway (shortening labels would be the way to go then).But I agree with @doug that for more fine grained control, lattice or ggplot2 should be considered.