3

After creating a Graphic plot with 4-5 graphs, I have been attempting to convert to a grid object to use grid.arrange to combine the Plots with output from Levelplot:

dev.off()
mat <- cbind(1:2,3:4,5:6)
layout(mat,nrow=1,ncol=6, byrow=TRUE) #= matrix(c(1,2,3,4,5,6)
par(mfrow=c(1,6))
par(mar = c(5, 4, 1, 1))
plot( 1-(outData[,(2)]), outData$depths,
 xlab = "1-R2Samples", 
 ylab = "depth", 
 ylim= (range(outData$depths)), ## to make this fail change to ylim= 
rev(range(outData$depths)),
 type = "l",
 pch = 5,
 cex = 1,
las=1,
asp = 0  
)
par(mar = c(5, 1, 1, 1))
plot(outData$Int, outData$depths,  
 xlab = "Volume Sum", 
 ylab = "", 
ylim=(range(outData$depths)), ## to make this fail change to 
ylim=rev(range(outData$depths)),
 type = "l",
 yaxt = "n",
 pch = 5,
 cex = 0.1
)
library(gridGraphics)
grab_grob <- function(){
grid.echo()
grid.grab()
}
p1 <-grab_grob()`

This issue I have been experiencing is in relation to converting graphics to grid.

With the graphics produced using Plot(), the Graphics window normally ends up with 5 graphs ordered as columns next to each other.

The grid.echo process fails, for some reason, because I need to reverse the y axes and I am struggling to work out why.

By failure, I receive this error message with reversed axes (I want to show 0 at top and and highest values at the bottom).

On the other hand, grid.echo works fine if I do not reverse the Y axis.

"Error in unit(ticks[ticksub], "native") :   'x' and 'units' must have length > 0 "

The data outData are XXX obs. of 14 variables and my aim is to graph selected variables against depth.

The grid.echo works if depths decrease down the y-axis (not the other way around).

structure(list(depths = c(0.005, 0.015, 0.025, 0.035, 0.045), 
               outr2 = c(0.803147038991965, 0.719125018822535, 
                         0.69839336799921,0.657247646400696, 0.744238996282677), 
               Int = c(-0.00000102997230239077, -0.00000241955173121919, 
                       -0.00000334081711621547, -0.00000194823979055884, -0.00000408144523677508), 
               AvPeat1 = c(0.258752895317787, 0.191682024387082, 0.162885203307831, 
                           0.171238890185635, 0.196734971227272), 
               AvLor0.7 = c(0.574080094004846, 0.663133691909617, 0.712370721311404, 
                            0.758247798201192, 0.794280590391875), 
               SGoo3.088 = c(0.167167010677367, 0.145184283703301, 0.124744075380765, 
                             0.0705133116131729, 0.00898443838085217), 
               `Int -CI` = c(-0.00000275976206093585, -0.00000431831516647923, 
                             -0.00000537600925543973, -0.00000384988709740556, -0.00000608770133458209), 
               `Int +CI` = c(0.000000699817456154306, -0.000000520788295959148, 
                             -0.00000130562497699121, -0.0000000465924837121146, 
                             -0.00000207518913896807), 
               `AvPeat -CI` = c(0.232586945280783, 0.159074674126924, 0.128594303046463, 
                                0.132733085135592, 0.164549253275563), 
               `AvPeat +CI` = c(0.284918845354791, 0.224289374647241, 0.197176103569199, 
               0.209744695235678, 0.228920689178982), 
               `AvLor -CI` = c(0.52198969187707, 0.59821994006912, 0.644105407748852, 
               0.681591576413949, 0.730206211723999), 
               `AvLor +CI` = c(0.626170496132622, 0.728047443750113, 0.780636034873956, 
               0.834904019988435, 0.858354969059752), 
               `SGoo -CI` = c(0.116467123729589, 0.0820033569829375, 0.0583010544268054, 
               -0.00409662840847729, -0.053379521619722), 
               `SGoo +CI` = c(0.217866897625146, 0.208365210423665, 0.191187096334724, 
               0.145123251634823, 0.0713483983814263), 
               .Names = c("depths", "outr2", "Int", "AvPeat1", "AvLor0.7", "SGoo3.088", 
               "Int -CI", "Int +CI", "AvPeat -CI", "AvPeat +CI", "AvLor -CI", "AvLor +CI", 
               "SGoo -CI", "SGoo +CI"), row.names = c(NA, 5L), class = "data.frame"))
  • Please share sample of your data using `dput()` (not `str` or `head` or picture/screenshot) so others can help. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – Tung Aug 27 '18 at 02:03
  • I have the same problem. Any success? – Raha Sep 12 '18 at 18:09

1 Answers1

0

My solution to the issue was to build the graphic horizontally, so switching the axes to have depth horizontal rather than vertical.

I also received this from the author of the Package, that it was related to a bug in 'gridGraphics', which is apparently fixed in the development version on github (https://github.com/pmur002/gridgraphics), but unfortunately that requires the development version of R.

He also provided a workaround in the meantime (negate the data and then negate the tick labels) ...

revDepths <- -(outData$depths)
par(mar = c(5, 4, 1, 1))
plot(1 - (outData[,(2)]), revDepths,
      xlab = "1-R2Samples",
      ylab = "depth",
      ylim= range(revDepths),
      type = "l",
      pch = 5,
      cex = 1,
      las=1,
      asp = 0,
      axes=FALSE)
axis(1)
ticks <- axTicks(2)
axis(2, at=ticks, labels=-ticks)

I did try this and struggled to get it to work, but by then I had made the graphics I needed by switching the axes and formatting the graphic to be portrait rather then landscape. However following through the suggestion above may help others.