'ggplot and scale_x_continuous
here a sample of data :
library(ggplot2)
lbreaks=c(-800,-600,-400,-300,-200,-100,-75,-50,-25,0,25,50)
ddf=data.frame(x=lbreaks,
y=seq(1,length(lbreaks),1))
and the scatter plot corresponding

I would use a custom scale (log or something similar for positive and negative values) for x-axis values to generate the same scatterplot with the equally spaced interval of x values. I tried to use several function in scale_x_continuous(trans = without sucess.
How could I do this ?
Many thanks for your help,
Solution 1:[1]
If you want the x axis breaks to be evenly spaced, the simplest thing to do is:
ggplot(ddf, aes(factor(x), y)) +
geom_point(colour = 'red') +
labs(x = 'x')
Perhaps less flippantly, your specific transformation can only really be achieved accurately by a piecewise transformation function:
my_trans <- scales::trans_new('custom',
transform = function(x) {
dplyr::case_when(
x < -400 ~ (x + 800) / 200 + 1,
x < -100 ~ (x + 400) / 100 + 3,
TRUE ~ (x + 100) / 25 + 6)
},
inverse = function(x) {
dplyr::case_when(
x < 3 ~ (x - 1) * 200 - 800,
x < 6 ~ (x - 3) * 100 - 400,
TRUE ~ (x - 6) * 25 - 100)
})
ggplot(ddf, aes(x, y)) +
geom_point(colour = 'red') +
scale_x_continuous(trans = my_trans, breaks = lbreaks)
This allows arbitrary data to be passed:
ddf=data.frame( x=rnorm(100,mean=-500,sd=200), y=rnorm(100,mean=0.5,sd=0.2) )
ggplot(ddf, aes(x, y)) +
geom_point(colour = 'red') +
scale_x_continuous(trans = my_trans, breaks = lbreaks)
However, such arbitrary x axis manipulations are not ideal, as they can be misleading.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



