【发布时间】:2015-07-06 19:56:26
【问题描述】:
我正在尝试生成一些分形,并且对 R 中 ggplot 的边距有疑问。我正在使用以下代码生成分形。
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.')
我正在寻找删除情节区域周围边距的想法。我尝试了一大堆技巧,例如在“par”中设置参数,xaxes / yaxes,last_plot() + labs(x=NULL, y=NULL) 等,但似乎没有任何效果。
有没有人想从情节中删除这个棘手的边缘?我还考虑过设置透明背景,但我必须剪掉边距——这是我想避免的步骤。
【问题讨论】:
-
尝试以
null为单位,即plot.margin = unit(c(0, 0, 0, 0), "null")