如何在R中制作像素图片

时间:2017-01-06 08:59:10

标签: r image plot

我有一个data.frame:

> head(shht)
  tt2 imf           f        a
1   1  c1 0.025735102 461.1430
2   2  c1 0.025735102 336.5967
3   3  c1 0.012977971 334.0539
4   4  c1 0.013013110 303.7454
5   5  c1 0.009130993 301.7031
6   6  c1 0.009153142 287.5305

数据为here

总共有四列。我想画一个像素图。第一列是水平轴,第三列是垂直轴,第四列是灰度值。

我尝试了以下内容:

> simage=as.image(shht$a,x=cbind(shht$tt2,shht$f))
> image.plot.ts(simage,tt=tt2)
Error in plot.new() : figure margins too large

image.plot.ts()是来自EMD的函数。 as.image()来自字段。

2 个答案:

答案 0 :(得分:1)

您可以通过生成数据矩阵来使用image函数吗?

x = rep(1:5,5)
y = rep(1:5, each = 5)
d = sample(1:10, length(x),replace = T)

df <- data.frame(x = x, y = y, d = d)

mat <- matrix(df[order(df$x, df$y),'d'], 5)
image(mat)

或者你可以制作一个简单的数据图,模仿像素。

plot(x, y, col=d, pch=15, cex=4)

编辑: 我不完全理解你的数据如何成为一个图像。由于每x坐标只有一个数据点:

id <- "0B3bNSNFik5wGNGY4VEQ0TFltM3M" 
dat <- read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id))

ggplot(dat, aes(x = tt2, y = f, color=a)) + geom_point() + theme_bw()

产生

enter image description here

答案 1 :(得分:0)

这是一个直接操作位图图像的包。 pixmap它有一系列创建和操作它们的方法。

一个例子,直接来自参考手册:

library(pixmap)


x <- seq(-3,3,length=100)
z1 <- outer(x,x,function(x,y) abs(sin(x)*sin(y)))
z2 <- outer(x,x,function(x,y) abs(sin(2*x)*sin(y)))
z3 <- outer(x,x,function(x,y) abs(sin(x)*sin(2*y)))


z <- pixmapRGB(c(z1,z2,z3), 100, 100, bbox=c(-1,-1,1,1))
plot(z, axes=TRUE)
相关问题