将文本对齐到R中具有可变大小的图 [英] Align text to a plot with variable size in R

查看:120
本文介绍了将文本对齐到R中具有可变大小的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用R的功能创建图形输出方面,我是一个新手.

I am very new in using the power of R to create graphical output.

我在metafor软件包中使用forest()函数来创建我的荟萃分析的Forest图.我使用循环生成了多个图,然后通过png()保存了它们.

I use the forest()-function in the metafor-package to create Forest plots of my meta-analyses. I generate several plots using a loop and then save them via png().

for (i in 1:ncol(df)-2)){
  dat <- escalc(measure="COR", ri=ri, ni=ni, data=df) # Calcultes Effect Size
  res_re <- rma.uni(yi, vi, data=dat, method="DL", slab=paste(author)) # Output of meta-analysis

  png(filename=path, width=8.27, height=11.69, units ="in", res = 210)
  forest(res_re, showweight = T, addfit= T, cex = .9) 
  text(-1.6, 18, "Author(s) (Year)", pos=4)     
  text( 1.6, 18, "Correlation [95% CI]", pos=2)
  dev.off()
}

如果图的大小相等,则效果很好.但是,循环的每次迭代都会在森林图中整合不同数量的研究.因此,文本元素不在正确的位置,并且经过大量研究的林区看上去有些奇怪.我有两个问题:

This works great if the size of the plot is equal. However, each iteration of the loop integrates a different number of studies in the forest plot. Thus, the text-elements are not on the right place and the forest-plot with many studies looks a bit strange. I have two questions:

  1. 如何自动将作者(年)"和相关性[95%CI]"与森林图的变化大小对齐,以使标题位于森林表的上方?
  2. 如何缩放森林图的大小,以使所有图的宽度和文本元素的大小都相同,并且对于每一项附加研究,仅添加一条新行(更改高度)?

每个森林图应如下所示:

Each forest-plot should look like this:

推荐答案

要使此功能正常运行,您需要执行以下操作:

Here is what you will have to do to get this to work:

  1. 我将在各个图中修复xlim,以便在固定位置放置作者(年)"和相关性[95%CI]"标题.生成森林图后,请查看par()$usr[1:2].使用这些值作为调整xlim的起点,使其适合您的所有绘图.然后将这两个值用于对text()的两次调用.

  1. I would fix xlim across plots, so that there is a fixed place to place the "Author(s) (Year)" and "Correlation [95%CI]" headings. After you have generated a forest plot, take a look at par()$usr[1:2]. Use these values as a starting point to adjust xlim so that it is appropriate for all your plots. Then use those two values for the two calls to text().

每个图中有k行.标题应在其上方两行.因此,请使用text(<first xlim value>, res_re$k+2, "Author(s) (Year)", pos=4)text(<second xlim value>, res_re$k+2, "Correlation [95% CI]", pos=2)

There are k rows in each plot. The headings should go two rows above that. So, use text(<first xlim value>, res_re$k+2, "Author(s) (Year)", pos=4) and text(<second xlim value>, res_re$k+2, "Correlation [95% CI]", pos=2)

text()中的cex设置为您在对forest()的调用中指定的值.

Set cex in text() to the same value you specified in your call to forest().

最后一部分很棘手.您已经固定了cex,因此文本元素的大小在各图中应相同.但是,如果有更多的研究,则k行将塞满较小的空间,因此它们之间的分隔变少了.如果我理解正确,则希望通过调整图的实际高度来使图之间的行间距相等.本质上,这将需要在对png()的调用中使height成为k的函数.对于每项额外的研究,都需要向height添加额外的量,以使行间距保持恒定,从而使height=<some factor> + res_re$k * <some factor>的内容保持不变.但是,高度随k的变化也可能是非线性的.正确执行此操作将需要大量的尝试和错误.可能有一种聪明的方法可以通过编程方式确定这一点(深入研究?par甚至是?strheight).

The last part is tricky. You have fixed cex, so the size of the text-elements should be the same across plots. But if there are more studies, then the k rows get crammed into less space, so they become less separated. If I understand you correctly, you want to keep the spacing between rows equal across plots by adjusting the actual height of the plot. Essentially, this will require making height in the call to png() a function of k. For each extra study, an additional amount needs to be added to height so that the row spacing stays constant, so something along the lines of height=<some factor> + res_re$k * <some factor>. But the increase in height as a function of k may also be non-linear. Getting this right would take a lot of try and error. There may be a clever way of determining this programmatically (digging into ?par and maybe ?strheight).

因此,让其他人更容易听到,您的问题的最后一部分归结为:如何调整绘图设备的height值,以使plot(1:10)plot(1:20)保持相等?这本身就是一个有趣的问题,因此我将把它作为一个单独的问题发布.

So make it easier for others to chime in, the last part of your question comes down to this: How do I have to adjust the height value of a plotting device, so that the absolute spacing between the rows in plot(1:10) and plot(1:20) stays equal? This is an interesting question in itself, so I am going to post this as a separate question.

这篇关于将文本对齐到R中具有可变大小的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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