Draw and fill the chess-like area (and analogies) in R

I need some suggestion on how to build 2D shapes and fill them efficiently in R I need to visualize some mathematical “oddities,” such as a Sierpiński pad or a simple chessboard, and get an effect similar to the following:

enter image description here

or

enter image description here

I was thinking about using complex numbers to define points, but still don't know how to fill in certain areas (for example, in two different colors).

+2
source share
2 answers

Something like that? Start with a matrix representing your data.

 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 

and then melt / graph:

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

enter image description here

Is this what you are looking for?

+8
source

geom_raster() will be the fastest for square tiles of the same shape.

You probably want geom_polygon() - here is Sierpinski:

don't forget to use coord_fixed(ratio=1) or the aspect ratio of the shape will scale to the shape of your viewer:

EDIT - sorry to understand that I did not give you Sierpinski (not sure what I thought) fixed

enter image description here

 require(ggplot2) require(reshape2) th<-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(nx-1,x)*th/n, x=seq((from=(0.5/n)+(nx)*(0.5/n)),by=1/n,length.out=x) ) }) points$id<-1:nrow(points) rbind( points, points+matrix(c((th/n),(-0.5/n),0),nrow(points),ncol=3,byrow=T), points+matrix(c((th/n),(0.5/n),0),nrow(points),3,byrow=T) ) } axiom<-data.frame(x=c(0,0.5,1),y=c(0,th,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") }) 

In addition, you should be aware of these very accurate recursive and serial models. Sometimes ggplot will not display the image exactly as you expect. For example, see below for the cantor dust diagram:

Using ggplot (using a raster) you can see that even at high resolution the “legs” look consistent, whereas mathematically you know what they are. If you upload an image and zoom in, you will see inconsistencies at the bottom.

enter image description here

In the code below, I showed how you can create your own accurate image by creating a raw png file on the fly. Do not be afraid to do this if you need precision! The result of the formation of the raw image below:

Good luck

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 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 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") 
+4
source

Source: https://habr.com/ru/post/1211455/


All Articles