Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid clipping of points along axis in ggplot

I am plotting a time series with mean values of a response variable as points (y-axis) by month (x-axis).

Values lying on the x-axis (i.e. 0 values) are clipped. I can change the limits of the y-axis to include some padding below 0, but I prefer not to.

Is there a way to plot these 0 points in front of, or on-top of the x-axis?

like image 826
user1267299 Avatar asked Mar 13 '12 19:03

user1267299


2 Answers

Try this,

q <- qplot(1:10,1:10,size=I(10)) + scale_y_continuous(expand=c(0,0))
gt <- ggplot_gtable(ggplot_build(q))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

clipoff

like image 165
baptiste Avatar answered Nov 01 '22 13:11

baptiste


With the release of ggplot2 version 3.0.0, you can simply use coord_cartesian(clip = 'off').

library(ggplot2)

qplot(x = 1:10, y = 1:10, size=I(10)) + 
  scale_y_continuous(expand=c(0,0)) +
  coord_cartesian(clip = 'off') +
  labs(title = "coord_cartesian(clip = 'off')")

enter image description here

If you're using one of the less commonly-used coord_* systems or functions (e.g. coord_polar or coord_flip), then you can use the clip = 'off' argument there, too.

my_plot + 
coord_flip(clip = 'off')
like image 15
bschneidr Avatar answered Nov 01 '22 15:11

bschneidr