Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading