Markus Germar
Markus Germar

Reputation: 223

How can I remove the strange white margin around my .png (plotted with r, ggplot)?

I save plots with ggplot as .png. The background has to be black, but there is allways a small white margin (only top, down an left; not right).

How can I remove this margin?

Thank you!

Here is my Code

library(ggplot2)
require(grid)


dat <- data.frame("xvar"=runif(500, 1, 10), 
              "yvar"=runif(500, 1, 10))

n <- 1
for(i in 1:n){
png(file=paste("green", i, ".png", sep=""), width=400, height=400)
  x <- sample(500, 50)
  i <- ggplot(data=dat[x,], aes(x=xvar, y=yvar))+
geom_point(col="green", size=3,shape=15)+
  theme(panel.background=element_rect(fill="black"), panel.grid.minor=element_blank(),
     panel.grid.major=element_blank(), axis.text.x=element_blank(), axis.text.y=
     element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(),
    axis.ticks=element_blank(), plot.background=element_rect(fill="black"), 
    panel.margin = unit(c(0,0,0,0), "cm"), plot.margin = unit(c(0,0,0,0), "cm"))+
  scale_x_continuous()
print(i)
dev.off() }

Example

enter image description here

Upvotes: 17

Views: 3916

Answers (1)

bdemarest
bdemarest

Reputation: 14667

The line you are seeing is the default outline colour of the plot.background rectangle element. You can remove it by setting colour to NA in your theme() call:

theme(plot.background=element_rect(fill="black", colour=NA))

Upvotes: 14

Related Questions