grid.cap()的替代方案,用于在R中旋转/倾斜图像图 [英] Alternative to grid.cap() for Rotating/Tilting an Image plot in R

查看:182
本文介绍了grid.cap()的替代方案,用于在R中旋转/倾斜图像图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到关于此的更多信息。我希望旋转主要情节和图像,同时保持图例和标题不变。

I can't seem to find much information on this. I would like to rotate the main plot in and image while leaving the legend and title unaltered.

这是一个例子。您需要字段包,它会为您提供 image.plot 功能。

Here's an example. You'll need fields package which gives you the image.plot function.

x=1:10
y=1:10
z=matrix(-50:49,10,10)
image.plot(x,y,z, main="Some Fancy Title",yaxt="n",xaxt="n", ylab="", xlab="")

所以我再次想要旋转主图像并保留标题和图例。顺便说一下,我正在旋转36度所以我不能只旋转我正在喂食的矩阵 image.plot 网格包似乎引导我朝着正确的方向前进,但令人沮丧的是我无法直接绘制到他们所谓的视口使用 base 中的情节或其他任何内容。到目前为止,我必须说不是 grid 包的粉丝。

So again I just want to rotate the main image and leave the title and legend in place. Btw I'm rotating 36 degrees so I can't just rotate the matrix I'm feeding image.plot. The grid package seems to lead me in the right direction but it is frustrating how I cannot plot directly into their so called viewports with plot or anything else from base. I must say not a fan of the grid package so far.

我发现这篇文章在回答5中提供了一些有用的信息。我从中知道我应该可以使用 grid.cap()捕获我的设备的栅格并使用 grid.raster(cap,vp = viewport(angle = 36))将绘图导入正确的视口。我在让这个方法为我工作时遇到了问题,启动它似乎效率很低。我将绘制数千个图像,以创建我已建模的各种时间和空间变化数据的GIF。

I found this post which provides some helpful information in answer 5. I know from this that I should be able to use grid.cap() to capture a raster of my device and use grid.raster(cap, vp=viewport(angle=36)) to import the plot into the correct viewport. I'm having problems getting this method to working for me and it seems pretty inefficient to boot. I will be plotting literally thousands of images to create gifs of various temporally and spatially varying data I have modeled.

我正在寻找的方法是获得 grid.cap()以使用上述方法例如,只旋转图像的主要部分36度,或做一个与我的体积图很好地相同的事情。非常感谢任何给出这个想法的人。

What I'm looking for is either a way to get grid.cap() to work with the above example and rotate only the main part of the image 36 degrees or an alternative doing the same thing that will work well with my volume of plots. Much thanks to anyone that gives this some thought.

推荐答案

问题在于 image.plot 不是一个情节而是两个:主要的一个和传说。如果你想使用包字段' image.plot 函数,你必须直接使用函数的代码。否则,您可以使用基本软件包 graphics 中的函数 image ,并手动添加您的图例和标题。

The issue is that image.plot is not one plot but two: the main one and the legend. If you want to use package fields' image.plot function, you'll have to fiddle with the function's code directly. Otherwise you can use function image from the base package graphics and add manually your legend and your title.

这是一种方法:

library(grid)
x=1:10
y=1:10
z=matrix(-50:49,10,10)

layout(matrix(c(1,2),ncol=2), widths=c(2,1))         
par(mar=c(5,3,5,3))
image(x,y,z,yaxt="n",xaxt="n", ylab="", xlab="",col=heat.colors(50)) 
cap <- grid.cap()
grid.newpage()
grid.raster(cap, x=unit(0.6,'npc'), #You can modify that if the plot 
            y=unit(0.5,'npc'),      #ends up outside the figure area
            vp=viewport(angle=36))
mtext("Some fancy title",side=3,cex=1.5,line=2) #Plot your title
par(mar=c(5,8,5,3))
plot(NA,ax=F,ann=F,type="n",xlim=c(0,1),ylim=c(0,50),yaxs="i")
for(i in 1:50)rect(0,i-1,1,i,col=heat.colors(50)[i],border=NA)
box()
axis(4,las=2,at=seq(0,50,by=10),labels=seq(-50,50,by=20))

这篇关于grid.cap()的替代方案,用于在R中旋转/倾斜图像图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆