本文介绍了完全没有边界的绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 png 图像文件上设置了一个带有透明叠加散点图的漂亮图.我希望我的绘图窗口和我的 pdf 输出与我的 png-962x745 的大小完全相同.

I set up a nice plot with a transparent superimposed scatterplot on a png image file. I want my plot window and my pdf output to be of the exact same size as my png- 962x745.

然而,即使在关闭轴、注释和框架之后,R 仍然在图像周围留下边框.

However, even after turning off axes, annotations and frames, R still leaves a border around the image.

这可以用一个简单的例子来说明:这个图显示了两个点,它们应该在图的最外端.但他们不是:

This can be shown with an easy example: This plot shows two dots, which should be at the outermost ends of the plot. But they aren't:

plot(rbind(c(1,745),c(962,1)),bty ="n",axes=F,frame.plot=F, xaxt='n', ann=FALSE, yaxt='n', asp=745/962)

与 PDF 设备一起使用:

And together with the PDF device:

pdf(width=10.02,height=7.76)
par(mar=rep(0, 4),mai=rep(0, 4), xpd = NA)
plot(rbind(c(1,745),c(962,1)),bty ="n",axes=F,frame.plot=F, xaxt='n', ann=FALSE, yaxt='n', asp=745/962)
dev.off()

推荐答案

你也可以使用 grid 图形来避开那些默认轴等

You could also use grid graphics to avoid those default axes, etc.

library(jpeg)
library(grid)

d = data.frame(x=rnorm(100, 10), y=rnorm(100, -100))
utils::download.file("http://i.imgur.com/5MexD.jpg", "img.jpg")
img = readJPEG("img.jpg")


w <- convertUnit(unit(ncol(img),"pt"), "in", value=TRUE)
h <- convertUnit(unit(nrow(img),"pt"), "in", value=TRUE)

dev.new(width=w, height=h)
grid.raster(img, width=unit(1,"npc"), height=unit(1,"npc"))

 v = dataViewport(xData=d$x, yData=d$y)
 grid.points(d$x,d$y, default.units="native", vp=v,
             gp=gpar(col="white"), pch=8)

这篇关于完全没有边界的绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:40