Rows using the R grid

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

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

It's good. If, however, I add the following line to the program:

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

Lines are everywhere. I expected the lines to connect the dots in order. I had a similar problem with some code that I wrote, but there the lines are good, but the dots do not.

Would thank for any help. Thanks.

+3
source share
1 answer

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 functions to a number vector without any related 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) 

A quick solution is to explicitly set the default units for grid.lines() according to the grid.points() :

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

enter image description here

+3
source

All Articles