在R中绘制并填充棋盘状区域(和类似物)

时间:2014-01-23 18:24:53

标签: r plot ggplot2

我需要一些关于如何绘制2D图形并在R中有效填充它们的建议。我需要想象一些数学上的“古怪”,例如Sierpiński垫片或简单的棋盘 - 并且收到类似于以下的效果:

enter image description here

enter image description here

我考虑使用复数来定义点,但仍然不清楚如何填充特定区域(例如,以两种不同的颜色)。

2 个答案:

答案 0 :(得分:8)

是这样的吗?从表示数据的矩阵开始

mx <- matrix(rep(c(T, F), 5), nrow=3, ncol=3)
#      [,1]  [,2]  [,3]
# [1,]  TRUE FALSE  TRUE
# [2,] FALSE  TRUE FALSE
# [3,]  TRUE FALSE  TRUE         

然后融化/绘图:

library(reshape2)
library(ggplot2)
ggplot(melt(mx), aes(x=Var1, y=Var2, fill=value)) + geom_tile()

enter image description here

这就是你要找的东西吗?

答案 1 :(得分:4)

对于形状相同的方形瓷砖,

geom_raster()将是最快的。

可能你想要geom_polygon() - 这是Sierpinski:

请记住使用coord_fixed(ratio=1),否则形状比例会缩放到观看者的形状:

编辑 - 对不起意识到看着它我没有给你Sierpinski(不确定我在想什么)修复

enter image description here

require(ggplot2)
require(reshape2)
t.h<-sin(2*pi/6)   # eq triangle unit height

sierpinski<-function(iter=3){
  n<-2^iter
  points<-ldply((n-1):0,function(x){
    data.frame(
      y=rep(n-x-1,x)*t.h/n,
      x=seq((from=(0.5/n)+(n-x)*(0.5/n)),by=1/n,length.out=x)
    )  
  })

  points$id<-1:nrow(points)

  rbind(
    points,
    points+matrix(c((t.h/n),(-0.5/n),0),nrow(points),ncol=3,byrow=T),
    points+matrix(c((t.h/n),(0.5/n),0),nrow(points),3,byrow=T)
  )
}

axiom<-data.frame(x=c(0,0.5,1),y=c(0,t.h,0))

iterations<-6

ggplot() + theme_bw() + coord_fixed(ratio=1) +
  geom_polygon(data=axiom,aes(x,y), fill="orange") + 
  lapply(1:iterations,function(x){
    geom_polygon(data=sierpinski(x),aes(x,y,group=id), fill="white")
  })

另外,你应该注意这些非常精确的递归和放大器。系列型号。有时,ggplot不会像您期望的那样提供精确的图像。例如请参阅下面的cantor粉尘图:

使用ggplot(使用光栅)你可以看到,即使使用高分辨率输出,“腿”看起来也不一致,而你在数学上知道它们是。如果你下载图像和缩放,你会看到底部的不一致。

enter image description here

在下面的代码中,我展示了如何通过动态创建原始png文件来生成自己的精确图像。如果你需要精确的话,不要害怕这样做!原始图像生成的输出如下:

祝你好运!

enter image description here

# CANTOR

# NUMBER OF ROWS
n<-9

# MATRIX
m<-matrix(sapply(n:1,function(x){
    str.zero<-rep(0,3^(x-1))
    str.one<-rep(1,3^(x-1))
    rep(c(str.one,str.zero),length.out=3^(n-1))
}),nrow=n,byrow=T)

# CLEANUP
m.cantor<-apply(m,2,cumprod)    

# ggplot

ggplot(melt(m.cantor)) + theme_bw() +
  geom_raster(aes(x=Var2,y=-Var1,alpha=value),color="white")

# MAKE IMAGE FROM RAW DATA
# LIBRARIES REQUIRED
require(png)

# AT THE MOMENT WE HAVE A SHORT, WIDE MATRIX
dim(m.cantor)
# [1]    9 6561
# so let's scale it up say * 700 to make an approx square image (9 x 700 = 6300 ~ 6561)
# basically we're running through each row and replicating x 700
# then putting the pixels in a matrix with the new height (n=9 * 700) 
new.m<-matrix(sapply(1:n,function(x)rep(m.cantor[x,],700)),nrow=n*700,byrow=T)

dim(new.m) # check the size
#[1] 6300 6561       *  OK!

# then let's put it in raw image format
# png expects an array of 4 matrices, R,G,B and alpha

img.raw<-array(c((1-new.m), # R,G,B pixels oinverted so 1=shading
                 (1-new.m),   # no G
                 (1-new.m),   # no B
                 new.m^0    #note I'm putting this to ^0 to make all alphas 1
                 ),
               dim=c(dim(new.m),4))


writePNG(img.raw,"cantor.png")