I am using R.
Suppose I have the following data:
#first data set
v2 <- c("A", "B", "C", "D", "E")
types <- as.factor(sample(v2,1000, replace=TRUE, prob=c(0.3, 0.2, 0.1, 0.1, 0.1)))
var <- rnorm(1000, 10, 10)
first = data.frame(v2, types, var)
#second data set
v2 <- c("A", "B", "C", "D", "E")
types <- as.factor(sample(v2,50, replace=TRUE, prob=c(0.3, 0.2, 0.1, 0.1, 0.1)))
var <- rnorm(50, 10000, 10)
second = data.frame(v2, types, var)
#final data
final = rbind(first, second)
#create transformed column
ihs <- function(x) {
y <- log(x + sqrt(x ^ 2 + 1))
return(y)
}
final$ihs = ihs(final$var)
I can now make plot the above data like this:
library(ggplot2)
library(ggridges)
ggplot(final, aes(x = ihs, y = types, fill = types)) +
geom_density_ridges() + ggtitle("my plot")
Is it possible to change the x-axis of the above plot so that it uses the scale from the "var" variable?
This would look something like this:
The "ihs" (inverse hyperbolic sine) value corresponds to "var" through the following relationship:
hs <- function(x) {
y <- 0.5*exp(-x)*(exp(2*x)-1)
return(y)
}
For example:
> ihs(70)
[1] 4.941693
> hs( 4.941693)
[1] 69.99997
Can someone please show me how to do this?
Thanks!
References:
You can achieve this by setting custom labels with scale_x_continuous()
.
library(ggplot2)
# install.packages('ggridges')
library(ggridges)
hs <- function(x) {
y <- 0.5*exp(-x)*(exp(2*x)-1)
return(y)
}
r <- \(x) round(x, 0)
ggplot(final, aes(x = ihs, y = types, fill = types)) +
geom_density_ridges() + ggtitle("my plot") +
scale_x_continuous(labels = r(hs(c(-5, 0, 5, 10))))
Note: use function(x)
instead of \(x)
if you use a version of R <4.1.0