在R中使用ggplot时,如何去除绘图区域周围的边距? [英] When using ggplot in R, how do I remove margins surrounding the plot area?

查看:3589
本文介绍了在R中使用ggplot时,如何去除绘图区域周围的边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ p

我试图生成一些分形,并且在R中有一个关于ggplot的边界的问题。我使用下面的代码来生成分形。
















b = 2,0.8,by = 0.005)
points = array(0,dim = c(length(step)^ 2,3))
t = 0

for(a in (n x = 0; y = 0; n = 0; dist = 0

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)
}

$ b $ df = as.data.frame(points)

ggplot(data = df,aes(V1,V2,color = cl [V3]))+
geom_point()+
主题(panel.background = element_blank(),
panel.g rid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin =单位(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,color = NA),
plot.margin = unit(c(0 ,0,0,0),cm),
legend.position ='none')

last_plot()+ scale_colour_manual(values = sort(c(#00000000,彩虹(35)),递减= FALSE))

ggsave('mandelbrot.png');
print('Image Saved。')

我正在寻找想法来移除地块周围的边缘。我尝试了一大堆技巧,比如在'par',xaxes / yaxes,last_plot()+ labs(x = NULL,y = NULL)等设置参数,但似乎没有任何效果。 p>

有人有想法从剧情中消除这个棘手的边际吗?我还打算设置一个透明的背景,但我不得不削减边际 - 我想避免的一个步骤。

解决方案

只从ggplot布局中选择绘图面板。它创建ggplot,将绘图面板中的元素设置为element_blank,并且不扩展x和y比例。然后创建ggplot grob,以便只能从布局中选择绘图面板。

轻微编辑:更新到ggplot2 2.2.0

 < 














$ step = seq(-2,0.8, ($)= $ 0.005)
points = array(0,dim = c(length(step)^ 2,3))
t = 0

for(a in step){$对于(b在步骤+0.6){
x = 0; y = 0; n = 0; dist = 0
而(n 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 (长度(cl)/ max_iter)
}
t = t + 1
points [t,] = c(a,b,color)
}
}

df = as.data.frame(points)

#ggplot将图表面板中的元素设置为element_blank()
#并且不在秤上进行扩展
p = ggplot(data = df,aes(V1,V2,color = cl [V3]))+
geom_point()+
scale_x_continuous(expand = c(0,0 ),limit = range(df $ V1))+
scale_y_continuous(expand = c(0,0),limits = range(df $ V2))+
theme(panel.grid = element_blank() ,
panel.background = element_rect(fill =transparent,color = NA),
panel.border = element_blank())+
scale_colour_manual(values = sort(c(#00000000 ,rainbow(35)),递减= FALSE))

#获取ggplot grob
gt = ggplotGrob(p)

#仅选择绘图面板
#gt = gt [6,4]#使用索引符号; OR
gt = gtable :: gtable_filter(gt,panel)

#绘制
grid.newpage()
grid.draw(gt)


#设置打印方法
class(gt)= c(Panel,class(gt))
print.Panel< - function(x) {
grid.newpage()
grid.draw(x)
}

gt
ggsave('mandelbrot.png',gt)


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.

解决方案

An approach that selects just the plot panel from the ggplot layout. It creates the ggplot, setting elements within the plot panel to element_blank, and no expansion of the x and y scales. It then creates the ggplot grob so that the plot panel only can be selected from the layout.

Minor edit: Updating to ggplot2 2.2.0

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 with elements in the plot panel set to element_blank()
# and no expansion on the scales
p = ggplot(data=df, aes(V1, V2, color=cl[V3]))+ 
  geom_point() + 
  scale_x_continuous(expand = c(0,0), limits=range(df$V1)) +
  scale_y_continuous(expand = c(0,0), limits=range(df$V2))+
  theme(panel.grid=element_blank(), 
       panel.background=element_rect(fill = "transparent",colour = NA),
       panel.border=element_blank()) +
  scale_colour_manual(values=sort(c("#00000000", rainbow(35)), decreasing=FALSE))

# Get the ggplot grob
gt = ggplotGrob(p)

# Select plot panel only
#   gt = gt[6,4]    # Using index notation; OR
gt = gtable::gtable_filter(gt, "panel")  

# Draw it
grid.newpage()
grid.draw(gt)


# Set up a print method 
class(gt) = c("Panel", class(gt))
print.Panel <- function(x) {
   grid.newpage()   
   grid.draw(x)
}

gt
ggsave('mandelbrot.png', gt)

这篇关于在R中使用ggplot时,如何去除绘图区域周围的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆