3

On the below plot, I'd like to create an empty space on the x-axis at position 3. In other word, I'd like that the dots for wt>=3 and the axis scale when wt>=3 are shifted to the right side by some arbitrary chosen value. Does it make sense?

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()

enter image description here

I could simply modify the my data and add 0.5 to each value of mpg where wt>=3 but it doesn't solve the issue with the x-axis.

I might rephrase my question saying that on the below graph I'd like that the vertical line does not overlap the data and therefore all the data (and also the x-axis) should be shifted to the left by the thickness of the vertical line.

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_vline(xintercept=3, size=30)

enter image description here

I was thinking about facet_wrap or viewport. Maybe something like adding a constant to each value of mpg where wt>=3 and then, manually set the values for the x-axis.

Remi.b
  • 17,389
  • 28
  • 87
  • 168
  • See [this](http://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis) related question on inserting a break in the axis. – Jota Nov 22 '13 at 17:44
  • The closest I got was using the plotrix package: `gap.plot(mtcars$wt,mtcars$mpg,gap=c(2.99999,3.000001),gap.axis="x")`. I didn't work on changing the gap size. – Jota Nov 22 '13 at 17:55
  • Alternatively, you could try an approach like this using ggplot2: `mtcars$foo<-ifelse(mtcars$wt >=3 ,c("over 3"),c("under 3"))` `mtcars$foo<-factor(mtcars$foo, levels=c("under 3","over 3"))` `ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + facet_wrap(~foo,scales="free_x")`. Though, it still needs some work. – Jota Nov 22 '13 at 18:02
  • 1
    There are many good answer. Thanks a lot! I accept Henrik's answer because he found the solution that is the closest to what has been asked but I haven't decided yet which solution I'll chose to present my data! – Remi.b Nov 23 '13 at 11:35

3 Answers3

2

Not entirely sure what you are looking for, and I get confused by watching the weird axis on my own plot... Something like this?

mtcars$wt2 <- with(mtcars, ifelse(wt > 3, wt+1, wt))

ggplot(mtcars, aes(x = wt2, y = mpg)) +
  geom_point() +
  annotate("rect", xmin = 3, xmax = 4, ymin = 0, ymax = 35, alpha = 0.2) +
  scale_x_continuous(breaks = round(mtcars$wt2), label = round(mtcars$wt))

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
1

Similar to @ Frank, and tested...

x <- mtcars
x$group <- ""
x[x$wt<3,]$group  <- "1.LIGHT"
x[x$wt>=3,]$group <- "2.HEAVY"

library(ggplot2)
library(grid)    #for unit(...)
ggplot(x, aes(x=wt,y=mpg)) + 
  geom_point() + 
  facet_grid(~group,scales="free",space="free") + 
  theme(panel.margin = unit(2, "lines"))

Produces this:

enter image description here

jlhoward
  • 58,004
  • 7
  • 97
  • 140
1

what about something like this?

mtcars <- transform(mtcars, split = ifelse(wt >= 3, "b", "a" ))

ggplot(mtcars, aes(x = wt, y = mpg)) + 
       geom_point() + 
       facet_wrap(~split, scales = "free_x")
user1317221_G
  • 15,087
  • 3
  • 52
  • 78