23

In a ggplot2 density plot (geom_density) I have the following y-axis labels

  • 0.000
  • 0.005
  • 0.010
  • 0.015
  • 0.020

What is the correct way to change them to something like

  • 0
  • 5
  • 10
  • 15
  • 20

possibly with the automatic adding of "10^3 x density" to the label. In the past I've just multiplied my data and manually changed the label, but in this case the y-axis data is generated for me by the density plot.

I'm aware that I can write things like scale_y_continuous(trans="log10"), but have not found any way to do a simple multiplicative constant, or define a custom transform.

Pengin
  • 4,692
  • 6
  • 36
  • 62

2 Answers2

38

This answer is out of date for ggplot2 version 0.90. Now, the same format would be specified (much more neatly) this way:

scale_y_continuous(labels=function(x)x*1000)

or if you want to use the same labelling scheme multiple times:

formatter1000 <- function(){
  function(x)x*1000
}

scale_y_continuous(labels=formatter1000())

Note that if you are specifying axis limits using the xlim and ylim functions, this might not work. Instead, use the scale_y_continuous(..., limits=c(0, 1)) specification.

There are also a bunch of built in formats in the scales package, including comma formatting, percentage formatting, dollar formatting and scientific notation formatting. See its documentation for more details.

Hope that helps someone out there, as this change certainly confused me!

fmark
  • 57,259
  • 27
  • 100
  • 107
  • Thanks. The documentation for the scales package is nothing but the details. What the scales package does is provide a large number of formatting functions like the `formatter1000` in this post, and to choose one of them you guess what it does from its function name. – Dave X Jun 06 '16 at 17:32
17

You could add the scale_y_continuous(formatter='formatter1000') with the following function defined before:

formatter1000 <- function(x){ 
    x*1000 
}

Please note, that the above answer was given one and a half year ago with a prior ggplot version. With the latest release (0.9) of ggplot the above example is not working, please try something like: scale_y_continuous(labels = formatter1000)

daroczig
  • 28,004
  • 7
  • 90
  • 124
  • That's perfect. Have you come across any good documentation on formatters? – Pengin Jan 10 '11 at 12:27
  • @Pengin: Unfortunatelly I do not. There are some hardcoded formatters in ggplot, e.g.: comma, scientific, percent, dollar, but you could write anything with simple functions like above. – daroczig Jan 10 '11 at 12:33
  • 1
    That's basically all there is too them, but in the next version they will be split out into their own package, hopefully with a bit more documentation. – hadley Jan 13 '11 at 04:13
  • 1
    Warning: This doesn't work as of `ggplot 0.9`. See my answer below for the updated version. – fmark Aug 08 '12 at 23:27
  • 1
    The `scale_y_continuous(labels = formatter1000)` note worked for me. Notice that the `labels' argument takes a function while the `formatter` option apparently takes the name of a function as a text string. – Dave X Jun 06 '16 at 17:25