Take the 2-minute tour ×

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),

ggplot(df, aes(x=x)) + 
  geom_bar(binwidth=0.5) + 
  facet_grid(~fac) + 

enter image description here

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.

share|improve this question

1 Answer 1

up vote 17 down vote accepted

You can pass in arguments such as min() and max() 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:

ggplot(df, aes(x=x)) + 
  geom_bar(binwidth=0.5) + 
  facet_grid(~fac) + 
  scale_x_continuous(breaks = c(min(df$x), 0, max(df$x))
    , labels = c(paste( 100 * round(min(df$x),2), "%", sep = ""), paste(0, "%", sep = ""), paste( 100 * round(max(df$x),2), "%", sep = ""))

or rotate the x-axis text with opts(axis.text.x = theme_text(angle = 90, hjust = 0)) to produce something like:

enter image description here


In the latest version of ggplot2 the breaks and labels arguments to scale_x_continuous accept functions, so one can do something like the following:

myBreaks <- function(x){
    breaks <- c(min(x),median(x),max(x))
    names(breaks) <- attr(breaks,"labels")

ggplot(df, aes(x=x)) + 
  geom_bar(binwidth=0.5) + 
  facet_grid(~fac) + 
  scale_x_continuous(breaks = myBreaks,labels = percent_format()) + 
  opts(axis.text.x = theme_text(angle = 90, hjust = 1,size = 5))
share|improve this answer
@Chase Thank you. Yes, I have considered doing this, but it isn't ideal. The reason is that the data could be percentages, respondent counts, t-stat scores, or whatever. Calculating the nearest magnitude might be an option, but really what I want to do is to use the scale that ggplot trained on, and then hide the labels between the end points. In other words, sometime I want the upper end of the scale to be (for example) 60%. I hope this makes sense. –  Andrie Mar 21 '11 at 17:30
@Andrie - got it. So what you really need here is a function that interprets the type of data shown on the x-axis (percentages, counts, etc...) and modifies the scale accordingly, right? Can you use class() on the columns to help inform this? Or some other data/metdata that informs what exactly you are plotting? It shouldn't be too difficult to write a small function to generate the vector of breaks and labels to pass into scale_x_continuous() assuming you have some info to inform what and how to format. –  Chase Mar 21 '11 at 17:50
@Chase I am hoping someone will provide a more generic approach. For example, when working with facets and free scales, e.g. facet_grid(~fac, scales="free"), the high and low break points will in general be different for each facet. So what I am really after is to suppress the labels without specifying the breaks. –  Andrie Mar 21 '11 at 20:49
@Andrie maybe you can provide an updated set of sample data that better illustrates your problem? From what I can tell, you have atleast two different issues. 1. Overplotting of the scale axis, 2. using the same code chunk to present the same data in different lights. You could address the overplotting with something like ... + opts(axis.text.x = theme_text(angle = 90, hjust = 0)). If you want to move beyond formatting issues, I think you are going to have to write your own function to pass parameters to the labels() and breaks(). –  Chase Mar 21 '11 at 21:26
+1 for suggesting changing the angle of text and size of text. This will help with my immediate presentational needs. –  Andrie Mar 21 '11 at 21:55

Your Answer


