如何很好地重新缩放晶格图形? [英] How to nicely rescale lattice figures?

查看:85
本文介绍了如何很好地重新缩放晶格图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在准备打印图形时,我经常需要使其比屏幕上显示的图形小得多.人物身高只有几厘米的情况并不少见.使用标准图形包par(cex=...)可用于按比例缩小整个图,以适合如此小的区域,并且看起来仍然成比例.晶格包装中有与之等效的东西吗?

When preparing figures for printing I often need to make them much smaller than they appear on screen. It is not uncommon to have figures down to just a few cm across. With the standard graphics package par(cex=...) can be used to scale down the entire plot to fit such a small area and still look proportional. Is there an equivalent to that in the lattice package?

基本上,如何在右侧面板的尺寸下绘制下图,但保留左侧面板的比例?

Basically, how do plot the figure below in the size of the right panel, but preserving the proportions of the left panel?

这个问题时不时地发生在我身上,通常我会疯狂地尝试trellis.par.gettrellis.par.set,然后放弃并使用标准图形重绘整个图形(非常乏味).当发现trellis.par.set(simpleTheme(cex=...))时,我以为我已经击中了金子,但它似乎无能为力.

This problem occurs to me every now and then, and it usually have me experimenting like mad with trellis.par.get and trellis.par.set before giving up and redrawing the entire figure using the standard graphics (which is very tedious). When discovering trellis.par.set(simpleTheme(cex=...)) I thought I had hit gold, but it doesn't seem to do anything.

我想重新调整绘图中的所有内容,包括字体大小,边距和刻度大小.很多.我知道我可以简单地调整pdf(height=..., width=...)以获得我喜欢的比例,然后在排版中重新缩放比例,例如是InDesign还是文字处理程序,但我认为如果每个pdf文件中都嵌入了预期的尺寸,我将得到更好的控制.

I want to rescale everything in the plot, including fontsizes, margins and tick sizes. The lot. I know I could simply adjust pdf(height=..., width=...) to get the proportions I like and then rescale it in the typesetting, e.g. InDesign or a word processor, but I feel I get better control if each pdf has its intended dimensions embedded in the file.

推荐答案

简介

有几种方法可以缩放格子图的元素大小.一种选择是更改所有具有cex属性的参数,另一种方法是更改​​将所有内容绘制到的基本fontsize.稍后您将看到所需的解决方案,但是了解如何通过par.settings参数影响图的各个组成部分也很有帮助,因为您可能希望调整应用于单个对象的相对缩放比例设置fontsize时的组件.

Intro

There are several ways in which the size of elements of a lattice plot can be scaled. One option is to change all the parameters that take a cex attribute, the other is to change the base fontsize to which everything is drawn. The solution you want is the latter as you'll see in a minute, but it is also instructive to see how you can influence individual components of the plot via the par.settings argument as you may wish to tailor the relative scaling applied to individual components when setting fontsize.

使用@Andrie的答案中的示例情节

Using the example plot from @Andrie's Answer

require(lattice)
plt <- xyplot(decrease ~ treatment, OrchardSprays, groups = rowpos,
              type = "a",
              auto.key = list(space = "right", points = FALSE, lines = TRUE))

设置fontsize参数,这是一个包含组件textpoints的列表,它们定义了这两个关键剧情元素的基本大小,从中可以生成所有其他剧情家具.

Set the fontsize parameter, a list with components text and points, which define the base sizes for these two key plot elements from which all other plot furniture is generated.

update(plt, par.settings = list(fontsize = list(text = 8, points = 4)))

(在150px x 200px PNG光栅设备上呈现).

(as rendered on a 150px by 200px PNG raster device).

需要注意的一件事是,莱迪思图的外观由trellis.par.get()

One of the things to notice is that the look of a Lattice plot is controlled by a whole stream of parameters as returned by trellis.par.get()

> thm <- trellis.par.get()
> str(thm, max = 1)
List of 35
 $ grid.pars        : list()
 $ fontsize         :List of 2
 $ background       :List of 2
 $ panel.background :List of 1
 $ clip             :List of 2
 $ add.line         :List of 4
 $ add.text         :List of 5
 $ plot.polygon     :List of 5
 $ box.dot          :List of 5
 $ box.rectangle    :List of 5
 $ box.umbrella     :List of 4
 $ dot.line         :List of 4
 $ dot.symbol       :List of 5
 $ plot.line        :List of 4
 $ plot.symbol      :List of 6
 $ reference.line   :List of 4
 $ strip.background :List of 2
 $ strip.shingle    :List of 2
 $ strip.border     :List of 4
 $ superpose.line   :List of 4
 $ superpose.symbol :List of 6
 $ superpose.polygon:List of 5
 $ regions          :List of 2
 $ shade.colors     :List of 2
 $ axis.line        :List of 4
 $ axis.text        :List of 5
 $ axis.components  :List of 4
 $ layout.heights   :List of 19
 $ layout.widths    :List of 15
 $ box.3d           :List of 4
 $ par.xlab.text    :List of 5
 $ par.ylab.text    :List of 5
 $ par.zlab.text    :List of 5
 $ par.main.text    :List of 5
 $ par.sub.text     :List of 5

其中许多都会影响绘制的对象和注释中使用的文本的大小.您需要修改所有适当的设置,并将仅这些设置作为列表传递给par.settings().这是缩放@Andrie的示例图的示例:

Many of which effect the size of the objects drawn and text used in annotations. You need to modify all of the appropriate settings and pass only those as a list to par.settings(). Here is an example of scaling @Andrie's example plot:

CEX <- 0.5

pset <- list(superpose.symbol = list(cex = CEX),
             plot.symbol      = list(cex = CEX),
             par.sub.text     = list(cex = CEX),
             par.main.text    = list(cex = CEX),
             par.zlab.text    = list(cex = CEX),
             par.ylab.text    = list(cex = CEX),
             par.xlab.text    = list(cex = CEX),
             dot.symbol       = list(cex = CEX),
             box.dot          = list(cex = CEX),
             add.text         = list(cex = CEX),
             axis.text        = list(cex = CEX))
plt2 <- update(plt, par.settings = pset)

使用 gridExtra 包在同一设备上排列两个晶格图,我们可以看到所有这些的效果:

Using the gridExtra package to arrange two lattice plots on the same device we can see the effect of all of this:

require("gridExtra")  ## loads package:grid too
grid.arrange(plt, plt2, ncol = 2)

导致

因此,现在(几乎)所有内容都已缩放.需要注意的一件事是,这种方法没有缩放图例中线条的长度或刻度线的长度.注意图的大小,每个子图的面积都相同.但是,我们可以将整个图绘制成更小的尺寸:

So now (almost) everything has been scaled. one interesting thing to note is that this approach hasn't scaled the length of the lines in the legend nor the tick lengths. Note the size of the plot the areas for each sub-plot were the same. We can however plot the whole thing at a much smaller size:

这是合理的,并且在基于矢量的设备(例如pdf())上呈现的效果要比我不得不在此处进行说明的栅格png()更好.第二个图是在150px x 200px的设备上.

Which is reasonable, and will look better rendered on a vector-based device (e.g. pdf()) than the raster png() I have had to use to illustrate here. The second plot is on a 150px by 200px device.

有一种更简单的方法,那就是通过fontsize参数设置文本和点的基本字体大小.默认值为:

There is a simpler way and that is to set the base fontsize for text and points via the fontsize parameter. The default of which are:

> trellis.par.get("fontsize")
$text
[1] 12

$points
[1] 8

这些会影响绘图中的所有内容,因此我们可以缩小它们以达到与设置所有其他设置类似的效果.

These affect everything on the plot, so we can shrink these to achieve a similar effect of setting all those other settings.

update(plt, par.settings = list(fontsize = list(text = 8, points = 4)))

产生

这是在与先前图2相同大小的png()设备上绘制的.请注意边距如何调整,以便更好地缩放绘图.因此,不仅影响了我们先前更改的所有设置,还影响了绘图的其他位(例如间距和刻度线),这些其他位也从fontsize设置中获取了它们的队列.同样,将它们绘制在矢量设备上会产生更好的输出.

This is plotted on the same sized png() device as Figure 2 from earlier. Notice how the margins have adjust so that the plot is better scaled. So not only have we affected all the settings we changed earlier, we have also affected the other bits of the plot (such as spacing and tick marks) that also take their queue from the fontsize settings. Again, plotting these on a vector device will produce better output.

这篇关于如何很好地重新缩放晶格图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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