Rstudio ggplot2 create own x-axis graduation

I’m looking to adapt the x-scale of my graph with the ggplot2 library

Here is a example of what I mean:

example <- data.frame(a = 1:2000, b = rnorm(n = 2000))
ggplot(example,aes(a,b)) + geom_point() + scale_x_continuous(n.breaks = 5)

Giving me this plot

enter image description here

What I want to do is to change the breaks of the x-axis so that its scale is not linear anymore.

As a example I’m looking to change the values of the breaks 500,1000,1500 to 2,20,200 respectively. The graph needs to adapt to this change of scale

I’m struggling to find the words to describe best the situation so I hope that you still understand

Thanks in advance

>Solution :

We can log transform an axis with base 10 using scale_x_log10, then use scales::trans_breaks to set your desired breaks.

library(tidyverse)
library(scales)

set.seed(123)
example <- data.frame(a = 1:2000, b = rnorm(n = 2000))

ggplot(example, aes(a, b)) + 
  geom_point() + 
  scale_x_log10(breaks = trans_breaks("log10", function(x) 2*10^x))

Created on 2022-07-26 by the reprex package (v2.0.1)

Leave a Reply