Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using ggplot in R, how do I remove margins surrounding the plot area?

Tags:

plot

r

ggplot2

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.

enter image description here

like image 474
jixtacom Avatar asked Jul 06 '15 19:07

jixtacom


People also ask

How do I change the margins on a plot in R?

To visualize how R creates plot margins, look at margin Figure 11.20. You can adjust the size of the margins by specifying a margin parameter using the syntax par(mar = c(bottom, left, top, right)) , where the arguments bottom , left … are the size of the margins. The default value for mar is c(5.1, 4.1, 4.1, 2.1).

How do you expand plot area in R?

Use par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area.


2 Answers

You can also use theme_nothing() from the cowplot package:

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)

Unfortunately, you still need to add labs(x = NULL, y = NULL), because there is no way in ggplot2's theme machinery to remove the axes completely. And you need to set expand=c(0,0) in the scale parameters to make sure the scale doesn't extend beyond your data range.

Result:

enter image description here

like image 127
Claus Wilke Avatar answered Sep 19 '22 15:09

Claus Wilke


After using your code, I see more clearly what you're looking for. This:

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

you have to remove the labels, not-expand the x & y axis and set hard limits. The nulls are also important.'

This can also be done by doing gb <- ggplotGrob(gg) and manually editing the grobs & parameters, but I think this probably gets you what you need.

like image 30
hrbrmstr Avatar answered Sep 20 '22 15:09

hrbrmstr