4

Using the example from Hadley's website:

> (m <- qplot(rating, votes, data=subset(movies, votes > 1000), na.rm = T))

Which creates:

example figure

My Question: Is it possible to determine what the ticks marks after creating the plot object? (I want to remove the first auto-generated breakpoint)

Background: In the above plot, one can clearly see that the x-axis breaks are at 2 through 9. To obtain this manually, use:

m + scale_x_continuous( breaks = c(2:9) )

But I would like to determine, from the figure, what the tick marks are so that I can remove some of them. In other words, is there a function which will return the tick marks:

myBreaks <- tickMarks(m)

So that I can subsequently call:

m + scale_x_continuous( breaks = myBreaks[-1] )

where I've removed the first break from the array.

M. Tibbits
  • 8,400
  • 8
  • 44
  • 59
  • It seems like you should be able to access the grid components that make up the plot. I don't have much/any experience doing so, but this post looks informative: http://rwiki.sciviews.org/doku.php?id=tips:graphics-ggplot2:edit-grob&rev=1243946778, Maybe someone will come along and give us a lesson. – Chase Jun 28 '11 at 04:39
  • I'm not sure what you mean, but simply, is it sufficient to set breaks manually after inspecting the figure, like this: `m + scale_x_continuous(breaks=3:9)` ? – kohske Jun 28 '11 at 05:04
  • I'm about to run a large simulation study where I'll be making roughly 1000 plots. **I need an automated solution** -- I'm actually plotting figures in a four by four [grid layout](http://learnr.wordpress.com/2009/05/26/ggplot2-two-or-more-plots-sharing-the-same-legend/) and the compressed viewports look smushed with soo many tick marks. – M. Tibbits Jun 28 '11 at 05:11
  • 2
    Your question is very similar to this question I posed a couple of months ago: http://stackoverflow.com/q/5380417/602276. Hadley responded by saying it's not easy to do it, but hopefully this will be possible in a future release of `ggplot2` – Andrie Jun 28 '11 at 06:58

1 Answers1

3

I'm not sure this is what you want, but you can do a hack by:

# drop first break
sx <- scale_x_continuous()
sx$.tr$input_breaks <- function(., range) grid.pretty(range)[-1]
m <- qplot(rating, votes, data=subset(movies, votes > 1000), na.rm = T)
m + sx

# reduce the breaks into half
sx$.tr$input_breaks <- function(., range) {
  r <- grid.pretty(range); r[seq_len(length(r)/2)*2]
}
m + sx

# set the (rough) number of breaks  
sx$.tr$input_breaks <- function(., range) pretty(range, 3)
m + sx

but note that this also affects y-axis...

And probably it is the easy way to make your own transform object.

TransIdentity2 <- Trans$new("identity2", "force", "force", "force")
TransIdentity2$input_breaks <- function(., range) pretty(range, 3)

m + scale_x_continuous(trans="identity2")

in this case, it does not affect y-axis.

kohske
  • 65,572
  • 8
  • 165
  • 155