Home > Blockchain >  Creating an Axis for a Plot in terms of another Variable
Creating an Axis for a Plot in terms of another Variable

Time:01-14

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")

enter image description here

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:

enter image description here

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:

  • enter image description here

    Note: use function(x) instead of \(x) if you use a version of R <4.1.0

  •  Tags:  
  • Related