ggplot generally does a good job of creating sensible breaks and labels in scales.
However, I find that in plot with many facets and perhaps a formatter= statement, the labels tend to get too “dense” and overprint, for example in this picture:
df <- data.frame(
fac=rep(LETTERS[1:10], 100),
x=rnorm(1000)
)
ggplot(df, aes(x=x)) +
geom_bar(binwidth=0.5) +
facet_grid(~fac) +
scale_x_continuous(formatter="percent")

I know that I can specify the breaks and labels of scales explicitly, by providing breaks= and scale= arguments to scale_x_continuous.
However, I am processing survey data with many questions and a dozen crossbreaks, so need to find a way to do this automatically.
Is there a way of telling ggplot to calculate breaks and labels automatically, but just have fewer, say at the minimum, maximum and zero point?
EDIT: Ideally, I don’t want to specify the minimum and maximum points, but somehow tap into the built-in ggplot training of scales, and use the default calculated scale limits.
You can pass in arguments such as
min()andmax()in your call to ggplot to dynamically specify the breaks. It sounds like you are going to be applying this across a wide variety of data so you may want to consider generalizing this into a function and messing with the formatting, but this approach should work:or rotate the x-axis text with
opts(axis.text.x = theme_text(angle = 90, hjust = 0))to produce something like:Update
In the latest version of ggplot2 the
breaksandlabelsarguments toscale_x_continuousaccept functions, so one can do something like the following: