问题
Using the example from Hadley's website:
> (m <- qplot(rating, votes, data=subset(movies, votes > 1000), na.rm = T))
Which creates:

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.
回答1:
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.
来源:https://stackoverflow.com/questions/6501538/r-ggplot2-is-it-possible-to-query-what-the-tick-marks-are-for-a-plot