6

I am using ggplot2 to make some simple figures with a series using coordinates to make rectangles along the x-axis. However, I can't seem to figure out how to remove the padding between the actual start of the plot (x=1 location) and the leftmost edge of the plot, at what would be (0,0). For example, I am using the following code:

library(ggplot2)
library(grid)
png(filename="sample.png", width=5600, height=70)
plot.data <- data.frame(start.points=c(1),end.points=c(10))
p <- ggplot(plot.data)
p + geom_rect(aes(xmin=start.points, xmax=end.points, ymin=0, ymax=1), fill="red") + theme_bw() + ylab(paste(sprintf("%60s", ""), "\nSample_label\n")) + theme(axis.title.y = element_text(size = 30, colour = "black", angle = 0))
dev.off()

The variables my_start and my_stop are just lists of start and stop coordinates for drawing rectangles. So, this plot creates a long horizontal figure, just as I want.

The problem is, the figure looks like this:

axis_label    | <-- white space --> <-----actual plot------> <-- white space--> |

Actual plot: enter image description here

...and I want to manually control this amount of white space. I've tried setting plot.margin and panel.margin, but these seem to control other aspects of the plot. Please note, that on my sample picture the red starts at point of 1 as expected. I want this to border the y axis, and I don't want any trailing space after the end of the red bar.

Any help would be greatly appreciated! Ideally I would just remove all white space padding. Thanks!

jake9115
  • 3,964
  • 12
  • 49
  • 78
  • 2
    This problem is not [REPRODUCIBLE](http://stackoverflow.com/help/mcve) as it contains no data. Also note that you can upload images. Thant may be a better way to discuss this topic than the purely text approach. – Tyler Rinker Apr 08 '14 at 18:09
  • 1
    I've added a single data point, so it now contains all of the data to reproduce the problem. I've also added a png of the actual plot to illustrate my problem more accurately. Please advise! – jake9115 Apr 08 '14 at 18:39

1 Answers1

13

Try to add to add scale_x_continuous(expand = c(0, 0)) to your plot code. From the help text:

expand: a numeric vector of length two, giving a multiplicative and additive constant used to expand the range of the scales so that there is a small gap between the data and the axes.

ggplot(plot.data) +
  geom_rect(aes(xmin = start.points, xmax = end.points, ymin = 0, ymax = 1), fill = "red") +
  scale_x_continuous(expand = c(0, 0))
Henrik
  • 65,555
  • 14
  • 143
  • 159