2
$\begingroup$

ggplot2 seems to, by default, select very nice, comprehensible breaks for continuous data. I would like to plot the data according to one scale (logistic) by having the axis labels in an easier-to-explain scale (percentage). I can make this plot by selecting breaks on the one scale and assigning labels according to the scale that is easier to explain.

What I haven't been able to manage is having the labels automatically fall onto sensible, comprehensible breakpoints. For example given data like this: x=c(0.09, 0.18, 0.27, 0.1, 0.2, 0.3, 0.11, 0.22, 0.33), ggplot will make a plot like this: qplot(rep(1:3,each=3),x)

enter image description here

which rounds the decimals to nice intervals of .05. I could manually force my data to round to nice intervals, but I would have to look at each plot individually and make that decision. I'm prone to batch generate my visualizations, so I'd like a way/method that will allow me to do this automatically. So, given a set of data, what is the best way to automatically select sensible, comprehensible breakpoints?

$\endgroup$

2 Answers 2

3
$\begingroup$

I'm pretty sure that, just like base graphics, ggplot using pretty() to generate break points. The help (?pretty) is straightforward. Using your example x:

> x=c(0.09, 0.18, 0.27, 0.1, 0.2, 0.3, 0.11, 0.22, 0.33)
> pretty(x)
[1] 0.05 0.10 0.15 0.20 0.25 0.30 0.35
> pretty(x, n = 3)
[1] 0.0 0.1 0.2 0.3 0.4

Notice that n is only a suggestion, and is the number of intervals, not the number of breaks.

If you'd like to modify this behavior, ggplot lets you pass a function to the breaks argument of any continuous_scale, see ?continuous_scale for details.

Edit:

Looked a little more... for the use case you describe you'll almost certainly want to look at the scales package (used internally by ggplot), especially ?pretty_breaks, ?logit_trans`, and maybe see this blog post about defining new transformations.

$\endgroup$
1
  • $\begingroup$ Thanks, pretty gets me most of the where I want to get to. I think you are right, that ggplot2/scales material probably would get me towards being able to get ggplot to do what I want inside ggplot. However, at the moment seems more difficult than shoehorning it from the outside. $\endgroup$ May 20, 2014 at 20:52
1
$\begingroup$

You don't really use breaks with logistic regression, but rather continuously plot $X$ vs fitted probability that $Y=1|X$.

In general the only variable for which discontinuous breaks exists is time (because events happen that immediately alter one or more variables) and age (because of legal mandates at ages 16, 18, 21, 65 for example).

$\endgroup$
2
  • $\begingroup$ Bad phrasing on my part, I wasn't really using "breaks", I just was selecting the position for major and minor grid-lines. The argument for (at least one way of) selecting those positions was "breaks". In the example I failed to plot it as a line chart just for expediency. $\endgroup$ May 20, 2014 at 20:18
  • $\begingroup$ Sorry I misinterpreted. $\endgroup$ May 20, 2014 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.