在R中使用ggplot时,如何删除绘图区域周围的边距?

jix*_*com 12 plot r ggplot2

我正在尝试生成一些分形,并在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.')
Run Code Online (Sandbox Code Playgroud)

我正在寻找消除情节区域边缘的想法.我尝试过一大堆技巧,例如在'par',xaxes/yaxes,last_plot()+ labs(x = NULL,y = NULL)等设置参数,但似乎没有任何效果.

有没有人有想法从剧情中删除这个难以处理的边际?我还考虑设置一个透明的背景,但我必须削减边距 - 这是我想避免的一个步骤.

在此输入图像描述

Cla*_*lke 11

您也可以使用theme_nothing()cowplot包:

require(cowplot)
qplot(1:10, (1:10)^2, geom='line') + theme_nothing() + 
  scale_x_continuous(expand=c(0,0)) +
  scale_y_continuous(expand=c(0,0)) +
  labs(x = NULL, y = NULL)
Run Code Online (Sandbox Code Playgroud)

不幸的是,你仍然需要添加labs(x = NULL, y = NULL),因为在ggplot2的主题机制中没有办法完全删除轴.您需要设置expand=c(0,0)比例参数以确保比例不超出您的数据范围.

结果:

在此输入图像描述


hrb*_*str 8

使用您的代码后,我会更清楚地看到您正在寻找的内容.这个:

gg <- ggplot(data=df, aes(V1, V2, color=cl[V3]))
gg <- gg + geom_point()
gg <- gg + labs(x=NULL, y=NULL, title=NULL)
gg <- gg + scale_x_continuous(expand=c(0,0), limits=range(df$V1))
gg <- gg + scale_y_continuous(expand=c(0,0), limits=range(df$V2))
gg <- gg + scale_colour_manual(values=sort(c("#00000000", rainbow(35)), decreasing=FALSE))
gg <- gg + theme(panel.background=element_rect(fill = "transparent",colour = NA))
gg <- gg + theme(plot.background=element_rect(fill = "transparent",colour = NA))
gg <- gg + theme(panel.grid=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(plot.margin=unit(c(0,0,0,0), "null"))
gg <- gg + theme(panel.margin=unit(c(0,0,0,0), "null"))
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text=element_blank())
gg <- gg + theme(axis.title=element_blank())
gg <- gg + theme(axis.line=element_blank())
gg <- gg + theme(legend.position="none")
gg <- gg + theme(axis.ticks.length=unit(0, "null"))
gg <- gg + theme(axis.ticks.margin=unit(0, "null"))
gg <- gg + theme(legend.margin=unit(0, "null"))
gg
Run Code Online (Sandbox Code Playgroud)

你必须删除标签,不要扩展x和y轴并设置硬限制.这null也很重要.

这也可以通过gb <- ggplotGrob(gg)手动编辑grobs和参数来完成,但我认为这可能会让你得到你需要的东西.


San*_*att 8

一种从ggplot布局中仅选择绘图面板的方法.它创建了ggplot,将绘图面板中的元素设置为element_blank,并且没有扩展x和y刻度.然后它创建ggplot grob,以便只能从布局中选择绘图面板.

次要编辑:更新到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)
Run Code Online (Sandbox Code Playgroud)


eip*_*i10 5

通过设置负绘图边距并将轴标题设置为NULL. 我在下面的代码中标记了编辑。

p = 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(-1, -1.2, -1.2, -1.5), "cm"),  # Edited code
        legend.position = 'none') +
  labs(x=NULL, y=NULL) # New code
Run Code Online (Sandbox Code Playgroud)