r - Remove Plot Margins in ggplot2 -
i'm trying produce png chart using ggplot2 , ggsave (with cairo) in r. i'm having issue customising theme remove margins.
currently i'm using:
... + theme(plot.margin=unit(c(0,0,0,0),"mm"))
this seems work 2 out of 4 sides of plot, removes top , right hand side margin completely, there still large margin on left , bottom sides. there way remove these? image below illustrate problem:
if reproducible example useful let me know , i'll put 1 together.
edit:
library("ggplot2") library("scales") library("cairo") library("grid") # set chart values line.width = 0.45 axis.font.size = 2.9 # generate random data start.date <- as.date("2011-07-01") x.month <-seq.date(start.date, = "month", length.out = 24) end.date <- max(x.month) period.a <- rnorm(12, mean=50, sd=2) period.b <- rnorm(12, mean=55, sd=2) x.value <- c(period.a,period.b) # combine dataframe x.data <- data.frame( "month" = x.month, "value" = x.value ) # build chart p <- ggplot(data=x.data, aes(month, value)) + geom_line(size=line.width) p <- p + theme_bw() p <- p + scale_y_continuous() p <- p + scale_x_date(limits=c(start.date+20,end.date-20), breaks = "1 month",labels = date_format("%b-%y")) p <- p + theme(axis.text.x=element_text(angle=90, hjust=1, vjust=0.5, size=axis.font.size), axis.text.y=element_text(size=axis.font.size), axis.title.x=element_blank(), axis.title.y=element_blank(), plot.margin=unit(c(0,0,0,0),"mm"), plot.background = element_rect(fill = "grey"), panel.grid=element_blank(), panel.border=element_rect(size=line.width/2), axis.ticks=element_line(size=line.width/3), axis.ticks.length=unit(0.3, "mm"), axis.ticks.margin=unit(0.2, "mm")) ggsave(file="c:\\temp\\test.png", plot=p, width=40, height=15, units="mm", type ="cairo-png")
according the source code, need set labels null,
last_plot() + labs(x=null, y=null)
alternatively, set unit(-0.5, "line")
bottom , left margins.
Comments
Post a Comment