Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty axis labels for log scale in ggplot

Tags:

r

ggplot2

When I try either of the following the axis scale is 1e+03, 1e+06, 1e+09 - is it possible to get nice superscripted 10^3, 10^6, 10^9 instead without resorting to manual labelling? I seem to recall getting this automatically in the past.

qplot(1:10, 10^(1:10))+scale_y_log10()
qplot(1:10, 10^(1:10), log='y')
like image 701
Sean Avatar asked Mar 02 '13 18:03

Sean


2 Answers

You can use trans_breaks() and trans_format() from library scales to get desired formatting of axis values.

library(scales)
qplot(1:10, 10^(1:10)) +
     scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
              labels = trans_format("log10", math_format(10^.x)))

enter image description here

like image 196
Didzis Elferts Avatar answered Nov 04 '22 06:11

Didzis Elferts


You can use label_log from the scales package:

library(scales)
qplot(1:10, 10^(1:10)) +
  scale_y_log10(labels = label_log())

enter image description here

like image 2
Maël Avatar answered Nov 04 '22 04:11

Maël