使用R grid包的行 [英] Lines using the R grid package

查看:252
本文介绍了使用R grid包的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R和cygwin,并试图绘制一些基本图形。这是一个Paul Murrell的论文中的一个简单例子:
$ b $ $ p $ library(grid)
x < - rnorm(50 )
y <-x + rnorm(50,1,2)
rx < - range(x)
dx <-diff(rx)
ry <范围(y)
dy < - diff(ry)
max < - max(rx,ry)
min < - min(rx,ry)
r < -c(min(rx,ry),max(rx,ry))
d <-diff(r)
比例<-r + c(-1,1)* d * 0.05
extscale <-c(min(scale),max(scale)+ diff(scale)* 1/3)
lay < - grid.layout(2,2,
widths =单位(c(3,1),英寸),
高度=单位(c(1,3),英寸))
vp1 < - 视口(w =单位(4, 英寸),h =单位(4英寸),
layout = lay,xscale = extscale,yscale = extscale)
grid.newpage()
pushViewport(vp1)
grid.rect()
grid.xaxis()
grid.text(Test,y = unit(-3,lines))
grid.yaxis()
grid.text(Retest,x = unit(-3,lines),rot = 90)
vp2 < - viewport(layout .pos.row = 2,layout.pos.col = 1,
xscale = scale,yscale = scale)
pushViewport(vp2)
grid.lines()
grid。点(x,y,gp = gpar(col =blue))

如果我在程序中添加以下行:

  grid.lines(x,y,gp = gpar(col =红色))

线条遍布整个地方。我曾预计线路将按顺序连接点。我遇到了一些类似的问题,我写了一些代码,但是这些代码行很好,但是代码没有。



希望得到任何帮助。感谢。

解决方案

由于某些原因, grid.points() grid.lines()具有不同的默认单位。 (这些单元分别是 - nativenpc> - 当您通过)

  args(grid.lines)
#function(x = unit (c(0,1),npc),y = unit(c(0,1),npc),
#default.units =npc,arrow = NULL,name = NULL, gp = gpar(),
#draw = TRUE,vp = NULL)
args(grid.points)
#function(x = stats :: runif(10),y = stats: :runif(10),pch = 1,
#size = unit(1,char),default.units =native,name = NULL,
#gp = gpar(),draw = TRUE,vp = NULL)

快速解决方案是显式设置<$ c的默认单位$ c> grid.lines()以匹配 grid.points()使用的值:

  grid.lines(x,y,gp = gpar(col =red),default.units =native)


I am using R with cygwin and am trying to plot some basic graphics. Here is a simple example from one of Paul Murrell's papers:

library(grid)
x <- rnorm(50)
y <- x + rnorm(50, 1, 2)
rx <- range(x)
dx <- diff(rx)
ry <- range(y)
dy <- diff(ry)
max <- max(rx, ry)
min <- min(rx, ry)
r <- c(min(rx, ry), max(rx, ry))
d <- diff(r)
scale <- r + c(-1, 1) * d * 0.05
extscale <- c(min(scale), max(scale) + diff(scale) * 1/3)
lay <- grid.layout(2, 2, 
                   widths = unit(c(3, 1), "inches"), 
                   heights = unit(c(1, 3), "inches"))
vp1 <- viewport(w = unit(4, "inches"), h = unit(4, "inches"), 
                layout = lay, xscale = extscale, yscale = extscale)
grid.newpage()
pushViewport(vp1)
grid.rect()
grid.xaxis()
grid.text("Test", y = unit(-3, "lines"))
grid.yaxis()
grid.text("Retest", x = unit(-3, "lines"), rot = 90)
vp2 <- viewport(layout.pos.row = 2, layout.pos.col = 1, 
                xscale = scale, yscale = scale)
pushViewport(vp2)
grid.lines()
grid.points(x, y, gp = gpar(col = "blue"))

This plots fine. If however I add the following line to the program:

grid.lines(x, y, gp = gpar(col = "red"))

The lines are all over the place. I had expected that lines would connect the points in order. I have had a similar problem with some code I wrote but there the lines plot fine but the points do not.

Would appreciate any help. Thanks.

解决方案

For some reason, grid.points() and grid.lines() have different "default units". (These are the units -- "native" and "npc" respectively -- that are used when you pass the functions a numeric vector without any associated units.)

args(grid.lines)
# function (x = unit(c(0, 1), "npc"), y = unit(c(0, 1), "npc"), 
#     default.units = "npc", arrow = NULL, name = NULL, gp = gpar(), 
#     draw = TRUE, vp = NULL)     
args(grid.points)
# function (x = stats::runif(10), y = stats::runif(10), pch = 1, 
#     size = unit(1, "char"), default.units = "native", name = NULL, 
#     gp = gpar(), draw = TRUE, vp = NULL) 

The quick solution is to explicitly set the default units for grid.lines() to match those used by grid.points():

grid.lines(x, y, gp = gpar(col = "red"), default.units = "native")

这篇关于使用R grid包的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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