seancarmody
seancarmody

Reputation: 6290

How do I adjust formatting of ggplot axis labels when using log-scales?

I have been having trouble taming the accuracy settings for a ggplot chart with a log scale. Using label_number doesn't quite give me the format I would like. The chart below shows a stylised example. Here, I would like to tweak the labels with one fewer decimal place, so they are 0.1, 1.0, 10.0, 100.0 and 1 000.0.

Simple log plot

ggplot(data = data.frame(x = 1:5, y=10^(1:5 - 2)), aes(x=x, y=y)) +
       geom_point() +
       scale_y_log10(labels = scales::label_number())

I though that the accuracy parameter would help me here, but it doesn't seem to behave nicely when using a log scale and ends up with odd behaviour. Here's an example - notice the strange appearance of the 2s at the end of the labels.

Strange log plot

ggplot(data = data.frame(x = 1:5, y=10^(1:5 - 2)), aes(x=x, y=y)) +
       geom_point() +
       scale_y_log10(labels = scales::label_number(accuracy=6))

Upvotes: 0

Views: 209

Answers (1)

Benedict Witzenberger
Benedict Witzenberger

Reputation: 182

Well, that looks pretty close.

Have you tried an accuracy of 0.1? (I took the idea from an example in the help file)

This works for me:

ggplot(data = data.frame(x = 1:5, y=10^(1:5 - 2)), aes(x=x, y=y)) +
  geom_point() +
  scale_y_log10(labels = scales::label_number(accuracy = 0.1))

Output: enter image description here

Upvotes: 4

Related Questions