I'm trying to generate some fractals and have a question regarding the margins with ggplot in R. I'm using the following code to generate the fractals.
library(ggplot2)
library(grid)
max_iter=25
cl=colours()
step=seq(-2,0.8,by=0.005)
points=array(0,dim=c(length(step)^2,3))
t=0
for(a in step) {
for(b in step+0.6) {
x=0;y=0;n=0;dist=0
while(n<max_iter & dist<4) {
n=n+1
newx=a+x^2-y^2
newy=b+2*x*y
dist=newx^2+newy^2
x=newx;y=newy
}
if(dist<4) {
color=24 # black
} else {
color=n*floor(length(cl)/max_iter)
}
t=t+1
points[t,]=c(a,b,color)
}
}
df=as.data.frame(points)
ggplot(data=df, aes(V1, V2, color=cl[V3]))+
geom_point() +
theme(panel.background=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin = unit(c(0, 0, 0, 0), "cm"),
axis.ticks=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
plot.background = element_rect(fill = "transparent",colour = NA),
plot.margin = unit(c(0, 0, 0, 0), "cm"),
legend.position = 'none')
last_plot() + scale_colour_manual(values=sort(c("#00000000", rainbow(35)), decreasing=FALSE))
ggsave('mandelbrot.png');
print('Image Saved.')
I'm looking for ideas to remove the margins surrounding the plot area. I've tried a whole bunch of tricks, such as setting parameters in the 'par', xaxes / yaxes, last_plot() + labs(x=NULL, y=NULL), etc., but nothing seems to work.
Does anybody have an idea to remove this intractable margin from the plot? I also contemplated setting a transparent background, but I'd have to cut out the margins - a step I'd like to avoid.