【问题标题】:R: using grid.echo to convert Plot to grid fails with reverse y axesR:使用 grid.echo 将 Plot 转换为网格失败,y 轴反向
【发布时间】:2018-08-27 00:24:55
【问题描述】:

在使用 4-5 个图形创建图形绘图后,我一直在尝试转换为网格对象以使用 grid.arrange 将绘图与 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 个变量,我的目标是根据深度绘制选定的变量。

grid.echo 可以在深度沿 y 轴(而不是相反)减小时起作用。

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

【问题讨论】:

  • 请使用dput()(不是str或head或图片/屏幕截图)分享您的数据样本,以便其他人可以提供帮助。在此处查看更多信息stackoverflow.com/questions/5963269/…
  • 我也有同样的问题。成功了吗?

标签: r plot


【解决方案1】:

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

我还从包的作者那里收到了这个,它与“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)

我确实尝试过这个并努力让它工作,但到那时我已经通过切换轴并将图形格式化为纵向而不是横向来制作我需要的图形。但是,遵循上述建议可能对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多