R 箱线图中自动调整标签

发布于 2024-08-14 03:46:20 字数 192 浏览 8 评论 0原文

我们目前正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡水深流 2024-08-21 03:46:20

Lattice 是最有可能在这里提供帮助的图形库。我这么说有两个原因:(i)点阵基于网格系统,通过访问网格的图形基元,您可以更好地控制面板输出的位置等。 (ii) 还有更多需要处理——R 标准图形包有 70 个不同的参数,而 Lattice 有 371 个——无论如何,根据我的计数,(length(names(unlist(trellis.par.get())))) ,然而这 371 个参数并不像基础包那样采用扁平结构,而是以分层结构收集(顶层有 30 个左右的参数组)。

您想要的是轴标签的相对定位。我建议针对此类任务降低一级。因此,要做你想做的事,只需更改相关的 grob 插槽,然后重新绘制两个 grob(使用 R 交互式提示):

library(lattice)
library(grid)
bwplot(~runif(200, 10, 99), xlab="x-axis label", ylab="y-axis label")
# move the x-axis label to the far left
grid.edit("[.]xlab$", grep=T, x=unit(0, "npc"), just="left", redraw=T)
# move it to the far right
grid.edit("[.]xlab$", grep=T, x=unit(1, "npc"), just="right", redraw=T)
# move it to the center
grid.edit("[.]xlab$", grep=T, x=unit(0.5, "npc"), just="center", redraw=T)
# same for y-axis
grid.edit("[.]ylab$", grep=T, y=unit(0.5, "npc"), just="center", redraw=T)

“[.].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):

library(lattice)
library(grid)
bwplot(~runif(200, 10, 99), xlab="x-axis label", ylab="y-axis label")
# move the x-axis label to the far left
grid.edit("[.]xlab$", grep=T, x=unit(0, "npc"), just="left", redraw=T)
# move it to the far right
grid.edit("[.]xlab$", grep=T, x=unit(1, "npc"), just="right", redraw=T)
# move it to the center
grid.edit("[.]xlab$", grep=T, x=unit(0.5, "npc"), just="center", redraw=T)
# same for y-axis
grid.edit("[.]ylab$", grep=T, y=unit(0.5, "npc"), just="center", redraw=T)

"[.].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.

南街九尾狐 2024-08-21 03:46:20

使用基本绘图系统,快速解决方案可能是使用 las=2las=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 or las=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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文