你如何在R中的多图环境中绘制一条线? [英] How do you draw a line across a multiple-figure environment in R?

查看:1097
本文介绍了你如何在R中的多图环境中绘制一条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举一个非常简单的例子, mfrow = c(1,3);每个图都是不同的直方图;我将如何绘制一个水平线(类似于 abline(h = 10)),这个水平线贯穿全部 3个数字? (即便是它们之间的差距)。显然,我可以为每个数字添加一个abline,但这不是我想要的。我可以想象一个非常复杂的方式来完成 ,只有1个数字,并使用多边形等等在其中绘制每个'图形'。那太荒谬了。是不是有一种简单的方法来做到这一点?

正如@joran指出的那样,网格图形化系统可以更灵活地控制单个设备上多个图的布置。

在这里,我首先使用 grconvertY()以标准化设备坐标为单位以y轴为单位查询高度为50的位置。 (即作为绘图装置总高度的一部分,其中0 =底部,1 =顶部)。然后,我使用 grid 函数来:(1)推入填充设备的 viewport ; (2)在由 grconvertY()返回的高度绘制一条线。

 <$ c 
par(mfrow = c(1,3))
barplot(VADeaths,border =dark blue)
barplot(VADeaths,border =黄色)
barplot(VADeaths,border =green)

##从第三个绘图中,获得
##的标准化设备坐标在y轴上为50。
(Y < - grconvertY(50,user,ndc))
#[1] 0.314248

##使用网格$ b $添加水平线b库(grid)
pushViewport(viewport())
grid.lines(x = c(0,1),y = Y,gp = gpar(col =red))
popViewport()


编辑:@joran询问如何绘制一条从y第一个图的轴向第三个图中最后一个条的边缘。这里有几个选择:

  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 )
X2 <-grconvertX(tail(m,1)+0.5,user,ndc)#条的默认宽度= 1
Y < - grconvertY(50,user ,ndc)

##水平线
pushViewport(viewport())
grid.lines(x = c(X1,X2),y = Y,gp = gpar(col =red))
popViewport()


最后,这里有一个几乎<等价的,更普遍有用的方法。它使用Paul演示的函数 grid.move.to() grid.line.to()> Murrell在@ mdsumner的回答中将文章链接到了:

  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))


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?

解决方案

As @joran noted, the grid graphical system offers more flexible control over arrangement of multiple plots on a single device.

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()

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()

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屋!

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