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!