1

I have data like this, with following plotting of the data (inspired from this post):

x_val <- 0:250
y_val <- c(seq(0,1, 0.1), 1:10)
set.seed(1234)
data <- data.frame(x = sample(x_val, 30, replace = TRUE),
                   y = sample(y_val, 30, replace = TRUE))
library(ggplot2)
p <- ggplot(data, aes(x, y)) + geom_point()
p + scale_y_continuous(breaks = seq(0, 10, by = 1))

I wish to have half of the y-axis from 0 to 1, and the other half of the y-axis from 1 to 10. Any way of doing this?

Attempts:

I tried

scale_y_continuous(breaks = c(seq(0, 1, 0.1), seq(1, 10, 1)))

, but it does not work. I'm puzzled as to how to attack this.

EDIT: Just for clarity, I desire the following plot

plot

  • 4
    You are looking for a `log` scale: `scale_y_log10()`, or `scale_y_continuous(trans = "log10")`. – Bas Apr 15 '20 at 06:45
  • @Bas: amazing, that fixes it! If you make an answer, I'll accept! Could you explain what this "trans" really does? It just manipulates the y-axis, but not the points in the scatter plot? – Berthrand Eros Apr 15 '20 at 08:25
  • 1
    Happy to help! Your question was clear and concise (the drawing was expecially informative). – Bas Apr 15 '20 at 09:15

1 Answers1

1

The scale you are drawing is not linear (the difference between 0 and 1 is not equal to the difference between 1 and 10, but the lines are equally far apart).
Therefore, you need to transform your data. In your case, you are looking for a log10 transform, since the distance between 0.1 and 1 on a log-scale equals the distance between 1 and 10 (note that 0 is not valid on a log-scale):

ggplot(data, aes(x, y)) + geom_point() + scale_y_log10()

Note that scale_y_log10 is the same as scale_y_continuous(trans = "log10"). This transforms your points to log-scale, while preserving the y-axis labels to be on the original scale.
Compare with

ggplot(data, aes(x, log(y))) + geom_point()

which transforms your points on a log-scale and also transforms the y-axis labels.

Bas
  • 4,628
  • 1
  • 14
  • 16