I have a plot I made with ggplot2 in R that I would like to add a horizontal text label to the y-axis. However, depending on the length of my text, R compresses my plot accordingly to create a fixed width image. However, I need the plots to be identical length and have identical starting and stopping positions (margins) no matter the text width.
I tried changing the plot margins by overwriting the default ggplot2 theme elements like so:
library(ggplot2)
png(filename="sample.png", width=5600, height=70)
plot.data <- data.frame(start.points=c(my_start),end.points=c(my_stop))
p <- ggplot(plot.data)
p + geom_rect(aes(xmin=start.points, xmax=end.points, ymin=0, ymax=1), fill="red") + theme_bw() + ylab("sample_title") +
theme(axis.title.y = element_text(size = 30, colour = "black", angle = 0), axis.text = element_blank(), legend.key = element_blank(), axis.ticks = element_blank(), plot.margin = unit(c(0.1, 0.1, 0.1, 12), "lines"))
dev.off()
So this makes a nice plot, but depending on my axis label the entire plot is stretched or compressed. Can someone please help me find a way to hold the plot width and margins static while changing the axis label text? If the axis label text is too long and it extends outside of the left-hand image border on into the plot itself, that is ok, as long as the plot doesn't change.
Thanks for any help! I've been banging my head on this for way too long.