R:使用grid.echo将Plot转换为网格失败,反向y轴

问题描述 投票:2回答:1

在创建一个包含4-5个图形的图形图之后,我一直在尝试转换为网格对象以使用grid.arrange将Plots与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()`

我遇到的这个问题与将图形转换为网格有关。

使用Plot()生成的图形,图形窗口通常最终会有5个图形作为彼此相邻的列排序。

由于某些原因,grid.echo进程失败,因为我需要反转y轴,我正在努力找出原因。

如果失败,我会收到反向轴的错误消息(我希望在顶部显示0,在底部显示最高值)。

另一方面,如果我不反转Y轴,grid.echo工作正常。

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

数据outData是XXX obs。 14个变量,我的目标是根据深度绘制所选变量的图形。

如果深度沿y轴向下减小(而不是相反),则grid.echo可以工作。

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"))
r plot
1个回答
0
投票

我对这个问题的解决方案是水平构建图形,因此切换轴的深度是水平而不是垂直。

我也从包的作者那里得到了这个,它与'gridGraphics'中的一个错误有关,这个错误显然是在github(https://github.com/pmur002/gridgraphics)的开发版本中修复的,但遗憾的是它需要R的开发版本。

在此期间,他还提供了一种解决方法(否定数据,然后否定刻度标签)......

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)

我确实试过这个并努力让它工作,但到那时我通过切换轴并将图形格式化为纵向而不是横向来制作我需要的图形。但是通过上面的建议可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.