I have some data with x-values on a 0-to-100 scale, like this:
library(ggplot2)
set.seed(42)
df <- data.frame(x=c(rep(100, 20), runif(100, min=0, max=100)),
y=rnorm(120, mean=4, sd=2))
A simple scatterplot, produced by this code:
ggplot(df, aes(x=x, y=y)) +
geom_point(size=5) +
theme(panel.grid.major=element_line(color='black'),
panel.grid.minor=element_line(color='black'),
panel.background=element_rect(fill='white'))
But to emphasize that x-values outside the 0-to-100 range are meaningless, I want to clip the x-axis and the horizontal gridlines exactly at x=0 and x=100. I'm given to understand that the proper way to do this is with expand, by adding scale_x_continuous(limits=c(0, 100), expand=c(0, 0)) to my ggplot object. The result:
This shortens the gridlines, but it also clips the scatterplot markers at the left and right margins, as well as the 100 label on the x-axis. Can I cut off just the x-axis and grid lines before the margin, but render the markers and axis labels as if the margin were still there?


geom_rangeframefromggthemespackage will achieve this. Look attheme_tufteexample and how to specify the limits ofgeom_rangeframe. - mikeck