如何将geom_raster()输出到光栅图像?
我正在做定量图像分析,并用ggplot2将结果可视化。输出包含原始图像中每个像素的一个数据点如何将geom_raster()输出到光栅图像?,r,ggplot2,R,Ggplot2,我正在做定量图像分析,并用ggplot2将结果可视化。输出包含原始图像中每个像素的一个数据点 geom_raster()很好地用R显示了我的数据。但是输出与结果相对应的光栅图像会很好。这样,我就可以使用一个轻量级的图像查看器(例如,feh)浏览多个衍生图像,像素将完美地排列在一起 有没有一种简单的方法可以将像素输出到图像文件,并且只输出像素?没有图例,没有轴,只有像素。假设我的数据.frame有行和列,并且所需的输出分辨率也是已知的。这里有一种方法: library(ggplot2) libra
geom_raster()
很好地用R显示了我的数据。但是输出与结果相对应的光栅图像会很好。这样,我就可以使用一个轻量级的图像查看器(例如,feh
)浏览多个衍生图像,像素将完美地排列在一起
有没有一种简单的方法可以将像素输出到图像文件,并且只输出像素?没有图例,没有轴,只有像素。假设我的数据.frame
有行
和列
,并且所需的输出分辨率也是已知的。这里有一种方法:
library(ggplot2)
library(reshape2) # for melt(...)
n <- 100
set.seed(1) # for reproducible example
img <- matrix(rnorm(n^2,30,3),nc=n)
gg <- melt(data.frame(x=1:n,img),id="x")
ggplot(gg) + geom_raster(aes(x=x,y=variable,fill=value))+
scale_x_continuous(expand=c(0,0))+ # get rid of extra space on x-axis
guides(fill=FALSE)+ # turn off color legend
theme(axis.text=element_blank(), # turn off the axis annotations
axis.ticks=element_blank(),
axis.title=element_blank())
库(ggplot2)
库(重塑2)#用于熔体(…)
n这里有一个方法:
library(ggplot2)
library(reshape2) # for melt(...)
n <- 100
set.seed(1) # for reproducible example
img <- matrix(rnorm(n^2,30,3),nc=n)
gg <- melt(data.frame(x=1:n,img),id="x")
ggplot(gg) + geom_raster(aes(x=x,y=variable,fill=value))+
scale_x_continuous(expand=c(0,0))+ # get rid of extra space on x-axis
guides(fill=FALSE)+ # turn off color legend
theme(axis.text=element_blank(), # turn off the axis annotations
axis.ticks=element_blank(),
axis.title=element_blank())
库(ggplot2)
库(重塑2)#用于熔体(…)
n感谢霍华德为我指明了正确的方向。还有一些缺少的成分——例如,如果没有labs(x=NULL,y=NULL)
,输出PNG将在底部和左侧有白色边框
我决定我的解决方案应该包括两部分:
创建一个ggplot对象来可视化我的数据。(此步骤与平常相同。)
调用一个通用函数来处理所有恼人的细节,这些细节是将绘图输出为像素完美PNG所必需的
这里有一个这样的函数
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()
}
BorderlessPlotPng感谢jlhoward为我指明了正确的方向。还有一些缺少的成分——例如,如果没有labs(x=NULL,y=NULL)
,输出PNG将在底部和左侧有白色边框
我决定我的解决方案应该包括两部分:
创建一个ggplot对象来可视化我的数据。(此步骤与平常相同。)
调用一个通用函数来处理所有恼人的细节,这些细节是将绘图输出为像素完美PNG所必需的
这里有一个这样的函数
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()
}
BorderlessPlotPng如果图像位于sp:::graster
类中,则可以使用plot
并将其保存为旧的png();dev.off()
way.grid.graster(…,interpolate=FALSE)
?如果图像位于sp:::graster
类中,则可以使用plot
并将其保存为旧的png();dev.off()
way.grid.graster(…,interpolate=FALSE)
?