symbiotic
symbiotic

Reputation: 373

Save png of ggplot2 object with no margins

I am writing a web application which needs precise dimensions for its figures. I decided to make the figures with ggplot2 because they require specialized text from R. I would like for the figures created to have no margins as they will be rotated via JavaScript. I used this page for an idea of how to cut down on the margins: https://kohske.wordpress.com/2010/12/25/drawing-on-full-region-in-ggplot2/ but was unable to print to a .png file without borders. Here's the sample code.

library(ggplot2)
library(gtable)

circle <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  data.frame(x = xx, y = yy)
}



dat <- circle(c(0,0),1,npoints = 1000)

plot1 <- ggplot(dat,aes(x,y)) + 
  geom_path() +
  theme(axis.text.y=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.ticks.length = unit(0,"null"),
        axis.ticks.margin = unit(0,"null"),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.position="none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.margin = unit(0,"null"),
        plot.margin = rep(unit(0,"null"),4),
        axis.ticks.length = unit(0,"cm"),
        axis.ticks.margin = unit(0,"cm"))

png("plot.png", width=434, height=434)
print(plot1)
dev.off()

This outputs a circle with a decent sized border. Let me be precise about what I would like, a png which is 434x434 px with a circle which has no borders (e.g. the diameter of the circle is 434px). I could create a larger file then crop it down, but I will be making ~50 of these graphics. Thanks for your help!

Upvotes: 2

Views: 2443

Answers (4)

baptiste
baptiste

Reputation: 77096

here's another option,

library(ggplot2)
library(grid)
p <- qplot(1,1, geom="blank")+annotation_custom(circleGrob())
png("test.png", width=434, height=434)
grid.draw(ggplotGrob(p)[3,4])
dev.off()

enter image description here

Upvotes: 0

DemetriusRPaula
DemetriusRPaula

Reputation: 387

library(ggplot2)
library(gtable)

circle <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  data.frame(x = xx, y = yy)
}

dat <- circle(c(0,0),0.5,npoints = 1000)

plot1 <- par(mar=c(0,0,0,0))
plot1 <- ggplot(dat,aes(x,y)) + 
  geom_path() +
  theme(axis.text.y=element_blank(),panel.margin = unit(c(0,0,0,0), "lines"),
        axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.ticks.length = unit(0,"null"),
        axis.ticks.margin = unit(0,"null"),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        legend.position="none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        title = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.margin = unit(0,"null"),
        plot.margin = rep(unit(0,"null"),4),
        axis.ticks.length = unit(0,"cm"),
        axis.ticks.margin = unit(0,"cm")) + scale_x_continuous(expand=c(0,0)) +
        scale_y_continuous(expand=c(0,0)) + labs(x=NULL, y=NULL, title=NULL)


png("plot.png", width=434, height=434)
print(plot1)
dev.off()

Upvotes: 0

hrbrmstr
hrbrmstr

Reputation: 78792

You can also do:

scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x=NULL, y=NULL, title=NULL) +

Upvotes: 2

eipi10
eipi10

Reputation: 93761

You can get rid of the margin by changing your plot.margin to this:

plot.margin = unit(rep(-1.25,4),"lines"),

Upvotes: 1

Related Questions