且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用geom_segment在ggplot中绘制'type = b'-将参数调整为轴比

更新时间:2023-12-03 10:43:22

好吧,除非您使用例如theme(aspect.ratio ...)coord_fixed()手动设置轴比,否则,因为绘图会根据设备的大小.

Well, unless you manually set the axis ratio with for example theme(aspect.ratio ...), or coord_fixed() you can't, since the plots adjust their positioning based on the size of the device.

要对此进行检查,您可以通过ggplotGrob(myplot)将图制作成gtable并查看布局,该图形对象是面板.

To check this, you can make your plot into a gtable by ggplotGrob(myplot) and look at the layout what graphical object is the panel.

g <- ggplot(pressure, aes(temperature, pressure)) +
  geom_point()

grobs <- ggplotGrob(g)

在该布局中,您可以看到面板的t(顶部)和l(左侧)位置.

In that layout you can see the t (top) and l (left) position of the panel.

head(grobs$layout)

   t l  b r z clip       name
18 1 1 12 9 0   on background
1  6 4  6 4 5  off     spacer
2  7 4  7 4 7  off     axis-l
3  8 4  8 4 3  off     spacer
4  6 5  6 5 6  off     axis-t
5  7 5  7 5 1   on      panel

您可以在上方看到该面板是列表中的第六个类别,其高度为7,宽度为5.

You can see above that the panel is the sixth grob in the list and has position 7 of the heights and position 5 of the widths.

grobs$widths[5]

[1] 1null

grobs$heights[7]

[1] 1null

面板的高度和宽度通常以null单位定义,这是一种特殊的单位,它告诉图形设备首先计算所有其他元素,然后使用剩余的空间放置null尺寸的图形元素.

The height and width of panels are usually defined in null units, which is a special unit that kind of tells the graphics device to calculate all other elements first and use the leftover space to place the null-sized graphical elements.

此外,该图具有一个respect参数,该参数告诉图形设备null单位之间的比率应为1:1还是***.当存在已知的宽高比时,或通过facet_grid(space = ..., scale = ...)参数将此参数设置为true.如果为respect == TRUE,则高度为2null且宽度为1null的笔触的长宽比为2.

Furthermore, the plot has a respect parameter that tells the graphics device wether the ratio between null units should be 1:1 or are free. This parameters is set to true when there is a known aspect ratio, or by facet_grid(space = ..., scale = ...) parameters. If respect == TRUE, then a grob with a height of 2null and a width of 1null will have an aspect ratio of 2.

grobs$respect

[1] FALSE

我不想给您留下所有坏消息,所以我要指出的是,您也可以使用gtable中的这些宽度和高度将它们设置为您喜欢的样式.

I don't want to leave you with all bad news so I'm gonna point out that you can also use these widths and heights in the gtable to set them to your liking.

grobs$widths[5]  <- unit(2, "cm")
grobs$heights[7] <- unit(5, "cm")
grid.newpage(); grid.draw(grobs)

这可以帮助您绘制出完美的type = b样式图.

Which could help you plot the perfect type = b style plots.

离题但切线相关,回到您先前的问题时,我还可以对绘图进行几何解释(未成功完成,所以没有发布),没有指向对点的技巧.高宽比也给我带来了很多麻烦,因为这些点的确切位置在设备尺寸上发生了变化.在底层,默认情况下用于制作图形对象(毛刺)的网格包使用归一化的父坐标(npc,请参见?unit).似乎朝着正确方向的事情是,将您命名为hyp - param的内容转换为与unit(hyp, "npc") - convertUnit(unit(param, "mm"), "npc", axisFrom = "y", typeFrom = "dimension")等效的内容(对于y轴,我已经在使用npc单位作为坐标了).现在我无法正确实现此功能,但是也许可以帮助您获得一些想法.

Off-topic but tangentially related, back at your previous question I also had a go at making the geometric interpretation of the plot (wasn't succesfull so didn't post it), without the point over point trick. I also had a lot of trouble with the aspect ratio, since the exact placing of the points changed on the device size. Under the hood, the grid package that makes graphical objects (grobs) by default use normalised parent coordinates (npc, see ?unit). A thing that seemed to be pointing in the right direction was converting what you named hyp - param with the equivalent of unit(hyp, "npc") - convertUnit(unit(param, "mm"), "npc", axisFrom = "y", typeFrom = "dimension") (for the y-axis, I was already working with npc units for my coordinates). Now I wasn't able to implement this properly, but maybe it would help you get some ideas.