如何输出geom_raster()到光栅图像? [英] How to output geom_raster() to a raster image?

查看:896
本文介绍了如何输出geom_raster()到光栅图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行定量图像分析,并用ggplot2可视化结果。输出包含原始图像中每个像素的一个数据点。

geom_raster()很好地将我的数据可视化为R 。但是输出对应于结果的栅格图像会很好。这样,我可以使用轻量级图像查看器(例如, feh )翻转多个派生图像,并且像素会完美排列。



是否有一种简单的方法可以将像素和像素仅输出到图像文件?没有传说,没有斧头,只有像素。假设我的 data.frame 的列有 col ,并且所需的输出分辨率也是已知的。

解决方案

感谢jlhoward指引我朝着正确的方向发展。还有一些缺失的成分 - 例如,没有 labs(x = NULL,y = NULL),输出PNG在底部和左边将具有白色边框。



我决定我的解决方案应该有两部分:


  1. 创建一个ggplot对象以可视化我的数据。 (这一步与往常一样。)

  2. 调用通用函数来处理所有烦人的细节,这些细节必须将该图输出为像素完美的PNG。 / li>

这是一个这样的函数。

  BorderlessPlotPng < - 函数(绘图,...){
#将ggplot2绘图写入没有边框的图像文件。

#参数:
#plot:一个ggplot2绘图对象。
#...:传递给png()函数的参数。
需要(网格)
png(type ='cairo',antialias = NULL,units ='px',...)
print(plot
+ theme(plot。 margin = unit(c(0,0,-0.5,-0.5),'line'),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
legend.position ='none')
+ scale_x_continuous(expand = c(0,0))
+ scale_y_continuous(expand = c(0,0 ))
+ labs(x = NULL,y = NULL)

dev.off()
}

为了看到它的实际应用,下面是一些合成数据的图表。 (为了演示目的,我将每个输出像素的像素宽度设置为10个像素。)

 #合成数据。 
width< - 64
height< - 48
d< - data.frame(row = rep(1:height,each = width),
col = rep 1:width,height),
x = rnorm(n = width * height))

#构建并打印图。
library(ggplot2)
plot < - (ggplot(data = d,aes(x = col,y = height + 1 - row,fill = x))
+ geom_raster()
+ scale_fill_gradient2()

pixel_size< - 10
BorderlessPlotPng(plot,
filename ='test.png',
width = width * pixel_size,
height = height * pixel_size)

输出:



当然,使用 pixel_size < - 1 运行会为您提供1:1的图像,您可以通过来回翻转来比较原始图像。


I'm doing quantitative image analysis, and visualizing the results with ggplot2. The output contains one datapoint for each pixel in the original image.

geom_raster() nicely visualizes my data in R. But it would be nice to output a raster image corresponding to the results. That way, I could flip through several derived images using a lightweight image viewer (e.g., feh), and the pixels would line up perfectly.

Is there an easy way to output the pixels, and only the pixels, to an image file? No legend, no axes, nothing but the pixels. Assume my data.frame has columns for row and col, and the desired output resolution is also known.

解决方案

Thanks to jlhoward for pointing me in the right direction. There are a few more missing ingredients -- for instance, without labs(x=NULL, y=NULL), the output PNG will have white borders on the bottom and left.

I decided my solution should have two parts:

  1. Craft a ggplot object to visualize my data. (This step is the same as usual.)
  2. Call a general-purpose function to take care of all the annoying details which are necessary to output that plot as a pixel-perfect PNG.

Here is one such function.

BorderlessPlotPng <- function(plot, ...) {
  # Write a ggplot2 plot to an image file with no borders.
  #
  # Args:
  #   plot:  A ggplot2 plot object.
  #   ...:  Arguments passed to the png() function.
  require(grid)
  png(type='cairo', antialias=NULL, units='px', ...)
  print(plot
        + theme(plot.margin=unit(c(0, 0, -0.5, -0.5), 'line'),
                axis.text=element_blank(),
                axis.ticks=element_blank(),
                axis.title=element_blank(),
                legend.position='none')
        + scale_x_continuous(expand=c(0, 0))
        + scale_y_continuous(expand=c(0, 0))
        + labs(x=NULL, y=NULL)
        )
  dev.off()
}

To see it in action, here's a plot of some synthetic data. (I made each output pixel 10 pixels wide for demonstration purposes.)

# Synthetic data.
width <- 64
height <- 48
d <- data.frame(row=rep(1:height, each=width),
                col=rep(1:width, height),
                x=rnorm(n=width * height))

# Construct and print the plot.
library(ggplot2)
plot <- (ggplot(data=d, aes(x=col, y=height + 1 - row, fill=x))
         + geom_raster()
         + scale_fill_gradient2()
         )
pixel_size <- 10
BorderlessPlotPng(plot,
                  filename='test.png',
                  width=width * pixel_size,
                  height=height * pixel_size)

Output:

Of course, running with pixel_size <- 1 would give you a 1:1 image, which you could compare to the original image by flipping back and forth.

这篇关于如何输出geom_raster()到光栅图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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