你如何在 R 中的多图形环境中画一条线? [英] How do you draw a line across a multiple-figure environment in R?
问题描述
举个很简单的例子,mfrow=c(1,3)
;每个数字都是不同的直方图;我将如何绘制一条横穿所有 3 个数字的水平线(类似于abline(h=10)
)?(也就是说,即使是它们之间的边距.)显然,我可以为每个图形添加一个 abline,但这不是我想要的.我可以想到一种非常复杂的方法来做到这一点,真的只有一个图形,并使用 polygon
等在其中绘制每个图形".那太荒谬了.难道没有简单的方法可以做到这一点吗?
Take a very simple example, mfrow=c(1,3)
; each figure is a different histogram; how would I draw a horizontal line (akin to abline(h=10)
) that went across all 3 figures? (That is, even the margins between them.) Obviously, I could add an abline to each figure, but that's not what I want. I can think of a very complicated way to do this by really only having 1 figure, and drawing each 'figure' within it using polygon
etc. That would be ridiculous. Isn't there an easy way to do this?
推荐答案
正如@joran 所指出的,网格 图形系统可以更灵活地控制单个设备上多个绘图的排列.
As @joran noted, the grid graphical system offers more flexible control over arrangement of multiple plots on a single device.
这里,我首先使用grconvertY()
来查询y轴上高度为50的位置以标准化设备坐标"为单位.(即作为绘图设备总高度的比例,0=底部,1=顶部).然后我使用 grid 函数来: (1) 推送一个填充设备的 viewport
;(2) 在 grconvertY()
返回的高度处绘制一条线.
Here, I first use grconvertY()
to query the location of a height of 50 on the y-axis in units of "normalized device coordinates". (i.e. as a proportion of the total height of the plotting device, with 0=bottom, and 1=top). I then use grid functions to: (1) push a viewport
that fills the device; and (2) plot a line at the height returned by grconvertY()
.
## Create three example plots
par(mfrow=c(1,3))
barplot(VADeaths, border = "dark blue")
barplot(VADeaths, border = "yellow")
barplot(VADeaths, border = "green")
## From third plot, get the "normalized device coordinates" of
## a point at a height of 50 on the y-axis.
(Y <- grconvertY(50, "user", "ndc"))
# [1] 0.314248
## Add the horizontal line using grid
library(grid)
pushViewport(viewport())
grid.lines(x = c(0,1), y = Y, gp = gpar(col = "red"))
popViewport()
编辑:@joran 询问如何绘制一条从第一个图的 y 轴延伸到第三个图中最后一个条形边缘的线.这里有几个替代方案:
EDIT: @joran asked how to plot a line that extends from the y-axis of the 1st plot to the edge of the last bar in the 3rd plot. Here are a couple of alternatives:
library(grid)
library(gridBase)
par(mfrow=c(1,3))
# barplot #1
barplot(VADeaths, border = "dark blue")
X1 <- grconvertX(0, "user", "ndc")
# barplot #2
barplot(VADeaths, border = "yellow")
# barplot #3
m <- barplot(VADeaths, border = "green")
X2 <- grconvertX(tail(m, 1) + 0.5, "user", "ndc") # default width of bars = 1
Y <- grconvertY(50, "user", "ndc")
## Horizontal line
pushViewport(viewport())
grid.lines(x = c(X1, X2), y = Y, gp = gpar(col = "red"))
popViewport()
最后,这里有一个几乎等价的、更普遍有用的方法.它使用了 Paul Murrell 在@mdsumner 的回答中链接的文章中演示的 grid.move.to()
和 grid.line.to()
函数:
Finally, here's an almost equivalent, and more generally useful approach. It employs the functions grid.move.to()
and grid.line.to()
demo'd by Paul Murrell in the article linked to in @mdsumner's answer:
library(grid)
library(gridBase)
par(mfrow=c(1,3))
barplot(VADeaths); vps1 <- do.call(vpStack, baseViewports())
barplot(VADeaths)
barplot(VADeaths); vps3 <- do.call(vpStack, baseViewports())
pushViewport(vps1)
Y <- convertY(unit(50,"native"), "npc")
popViewport(3)
grid.move.to(x = unit(0, "npc"), y = Y, vp = vps1)
grid.line.to(x = unit(1, "npc"), y = Y, vp = vps3,
gp = gpar(col = "red"))
这篇关于你如何在 R 中的多图形环境中画一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!