2

Coming from this example (pasted here)

trans_cube <- trans_new(name = "cube root",
                        transform = cube_root,
                        inverse = cube)

# dummy data
plot_data <- data.frame(x = 1:10,
                        y = cube(1:10))

# without applying a transform
ggplot(plot_data, aes(x = x, y = y)) +
  geom_point()
# applying a transform
ggplot(plot_data, aes(x = x, y = y)) +
  geom_point() +
  coord_trans(y = trans_cube)

Why the simple transformation of just adding a constant (using scales::trans_new) won't work?

trans_add <- trans_new(name = "add",
                         transform = function(x) x + 200,
                         inverse = function(x) x -200)
 
ggplot(plot_data, aes(x = x, y = y)) +   geom_point() +  
coord_trans(y = trans_add )

The reason I'm asking is because I would like to back transform my y-axis and I need to do it using coord_trans or scale_y_continuous or similar.

UPDATE

I managed to show the back transformation with custom breaks by using the following, however I would like to know why the trans_new doesn't work.

fun_sc_back <- function(x){x*187+266} # transform 
fun_sc <- function(x){(x-266)/187}    # inverse 

p_tr + 
  scale_y_continuous(breaks =fun_sc(c(250, 500, 750)), labels=fun_sc_back)
Lefty
  • 368
  • 4
  • 11

0 Answers0