【问题标题】:Issue using layout() with one figure having multiple layers in R使用 layout() 的问题,其中一个图形在 R 中具有多个图层
【发布时间】:2015-01-27 04:23:45
【问题描述】:

我在使用 layout() 时遇到了一些严重的问题,当添加一个具有多个图层的图形时,这让我发疯了。

在我尝试创建的 jpeg 中的一个图形上添加图层时,我似乎只是遇到了问题。

布局是 1) 在顶部有一个简单的线图,用于时间序列的捕鱼数据,在底部 2) 有一个更大的海洋数据层地图图像。

我正在使用 library TeachingDemos 中的 image.plot() plus contour(... add=T) 和箭头 my.symbols(... add=T) 制作一系列地图。

数据是从大型 netCDF 文件中切分出来的。

图像和轮廓是溶解氧深度,红色箭头是表面电流。

下面是我的R代码,数据通过变量'n'循环:

   jpeg(paste(interpdate[n],"DailyDOLayerCenAm.jpg", sep=""), width=1150, height=1000, res=100)

   layout(matrix(c(1,2),nrow=2), heights=c(1,3))

   #first plot on the top, fish catch data by time, moving each day
   par(mar=c(1,4,.3,.5)) 

   plot(Date[15:n],sail$X7.day.Average[15:n], xlim=c(Date[15],Date[350]), 
    xlab='',ylab='Raises/Trip',ylim=c(0,50), type='l', xaxt='n', lwd=2.5)
    axis(1, Date, format(Date, "%b %d"), cex.axis = 1)
   abline(18.4,0, lty=2)
   points(Date[n],sail$X7.day.Average[n], pch=21, col='black', bg='red',    
   cex=3)

   #second plot, Ocean data

   par(mar=c(3,3.7,.5,1))

   # layer 1 plot the main layer, interpolated grid O2 minimum depth
   image.plot( as.surface( expandgrid, ww),xlim=c(xmin,xmax),     
    ylim=c(ymin,ymax), ylab="Latitude", xlab="Longitude",main="",
    col=pal(256), legend.lab="Depth of O2 Minimum Layer (m)",
   zlim=c(20,zlimit), cex=1.5)

   #layer 2 add the contours
   contour(as.surface( expandgrid, ww),xlim=c(xmin,xmax), ylim=c(ymin,ymax),   
   col='white', lwd=2, nlevels=10, labcex=1, add=T)

    #layer 3 add current arrows
    my.symbols(lonx,laty,ms.arrows, angle=theta, r=intensity, length=.06,      
    add=T,xlim=c(xmin,xmax), ylim=c(ymin,ymax), lwd=2, col="red", 
    fg="black")

    #layer 4add the map of land/countries
    plot(newmap, col="GREY", add=T)

    #add a point of home port in Guatemala
    points(-90.81, 13.93, pch=21, col='black', bg='yellow', cex=3.5)

    dev.off()

当我只绘制海洋数据时,它绘制得很好: https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xap1/t31.0-8/10861076_10101738319375937_3436888929444896713_o.jpg

在 layout() 中绘制它我弄得一团糟,轮廓线在 x 空间内很好,但在 y 空间内被压扁,箭头和地图叠加层也是如此: https://scontent-b.xx.fbcdn.net/hphotos-xfp1/t31.0-8/10923795_10101738319625437_7583695382864373718_o.jpg

【问题讨论】:

  • 我假设您使用的是来自fields pacakge 的imaage.plot()。为了让image.plot() 绘制图例,它需要控制布局。它似乎与 layout() 选项不兼容。因为您没有共享任何示例输入数据,所以您的问题不是 reproducible,因此很难提供解决方法。
  • 数据直​​接来自服务器上的 netCDF 文件,按时间/纬度/经度切片,我只是希望有人可能有使用 layout() 和 image.plot() 添加轮廓的经验.没有图例的 image() 会起作用吗....
  • 嗯,你真的需要澄清你正在使用哪个image.plot() 函数。了解可能的情况需要查看您尝试使用的绘图功能的来源。
  • 如何使用基于latticerasterVis 包?
  • @MrFlick object<-list(x=x,y=y,z=ExpandDOmindepth[,,n]) expandx<-seq(min(x),max(x),,length(x)*10) expandy<-seq(min(y),max(y),,length(y)*10) expandgrid<-make.surface.grid(list(expandx,expandy)) ww<-interp.surface(object,expandgrid) image.plot( as.surface( expandgrid, ww),xlim=c(xmin,xmax), ylim=c(ymin,ymax), ylab="Latitude", xlab="Longitude",main="", col=pal(256), legend.lab="Depth of O2 Minimum Layer (m)", zlim=c(20,zlimit), cex=1.5)

标签: r layout layer


【解决方案1】:

我通过不使用 imageplot() 而是使用 image() 并添加 Aurélien Madouasse 描述的颜色图例来解决此问题:

https://aurelienmadouasse.wordpress.com/author/aurelienmadouasse/

我从大量海洋数据中循环图像,并在一个函数中创建了一系列图。

我的代码(无数据)在这里,来自一个循环:

jpeg(paste(interpdate[n],"DailyDOLayerCenAm.jpg", sep=""), width=1150, 
height=1000, res=100)

layout(matrix(c(1,2),nrow=2), heights=c(1,3))

#first plot on the top, fish catch data by time, moving each day
par(mar=c(1,4,.3,.5)) 

plot(Date[15:n],sail$X7.day.Average[15:n], xlim=c(Date[15],Date[350]), 
   xlab='',ylab='Raises/Trip',ylim=c(0,50), type='l', xaxt='n', lwd=2.5)
axis(1, Date, format(Date, "%b %d"), cex.axis = 1)
abline(18.4,0, lty=2)
points(Date[n],sail$X7.day.Average[n], pch=21, col='black', bg='red', cex=3)

#second plot, Ocean data
#plot the main layer, dissolved oxygen minimum depth 
#plot the main layer, dissolved oxygen minimum depth 
image( as.surface( expandgrid, ww),xlim=c(xmin,xmax), 
  ylim=c(ymin,ymax),ylab="Latitude", xlab="Longitude",main="", col=pal(256), 
  zlim=c(20,zlimit), cex=1.5)


#add the contours
contour(as.surface( expandgrid, ww),xlim=c(xmin,xmax), ylim=c(ymin,ymax), 
col='white', lwd=2,levels=seq(0,zlimit,10), labcex=1, add=T)

#add sea surface current arrows
 my.symbols(lonx,laty,ms.arrows, angle=theta, r=intensity, length=.06, 
  add=T, xlim=c(xmin,xmax), ylim=c(ymin,ymax), lwd=2, col="red", fg="white")

#add the map of land/countries
  plot(newmap, col="GREY", add=T)

 #add a point of home port in Guatemala
 points(-90.81, 13.93, pch=21, col='black', bg='yellow', cex=3.5)
 colr <- pal(256) # colors from 'blues'
 legend.col(col = colr, lev = ww) # legend from Aurélien Madouasse:
 mtext("Depth of O2 Minimum Layer (m)", 4, line=2.5, font=2)
 dev.off()

要查看我正在制作成电影的新图像的情节,请参阅此链接: https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xpa1/t31.0-8/10856647_10101738461765587_2760202217270038911_o.jpg

【讨论】:

    猜你喜欢
    • 2022-09-30
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多