Reputation: 2500
I am trying to produce a scatter plot using ggplot which has a continuous x-axis, but with a huge time gap between two of the points. Time points are 20, 40, 60, 90, 120, 500, 520. They need to be continuous because I want the first 5 values to be scaled, but then I want a break in the axis between 120 and 500. I am aware of the problems with axis breaks more generally, (this SO question), but my question refers to the x axis not y axis.
Example data;
EG.df <- data.frame(AGE = c(20, 20, 40, 40, 60, 60, 90, 90, 120, 120, 500, 500, 520, 520),
S1 = c(28, 30, 21, 15, 18, 19, 18, 21, 16, 17, 21, 21, 19, 13))
ggplot(EG.df, aes(x=AGE, y=S1)) +
geom_point(size=10)
I think it might be possible with the plotrix package (? - I've never used it), but I would prefer to use ggplot. Any suggestions?
Upvotes: 1
Views: 1287
Reputation: 1694
Here is a really quick stab at it using only ggplot2
. I am using dplyr
only for the manipulation, so not really needed.
We first break up the data into the parts you'd like to plot. This can probably be done automatically using the data, but here I set the cuts manually. The key is to create two groups (before/after split).
Now because you have a data point in the gap you'd like to cut I used filter to not show that facet. Doesn't feel so clean.
EG.df <- EG.df %>%
mutate(cut = cut(AGE, breaks = c(0,120,500,600))) %>%
filter(cut != "(120,500]")
Now when we plot we use facets to create the gap. This means we will have a facet strip with the cut label. The key is using scales="free_x"
to make the x-axis auto-adapt.
ggplot(EG.df, aes(x=AGE, y=S1)) +
+ geom_point(size=10) + facet_grid(.~cut, scales="free_x")
I kept the size of the points as is. You can remove the strips by adding a theme.
theme(
strip.text.x = element_blank(),
strip.background = element_blank())
You'll notice that the two axis labels cut into each other. I'll leave that as an exercise for the reader to fix the axes.
Upvotes: 1