0
votes

Most tutorials I've seen for dealing with transforming log-based data involve using a log-based y-axis or x-axis scale. If I wanted to plot the log10 -based values of my data and indicate their relative actual exponent-based real values:

library(ggplot2)
library(scales)

a <- c(5,10,15,20)
b <- c(100,210,350,750)
d <- log10(b)

test_df <- data.frame(xval = a,yval = d)
test_plt <- ggplot(data = test_df,aes(x = xval,y = yval)) +
  geom_point() + 
  scale_y_continuous(breaks = seq(1,3,by = 1),limits = c(1,3),labels = trans_format("log10", 
math_format(10^.x)))

print(test_plt)

Based on the code, I get the following results:

Log10-based plot

Obviously, R is trying to convert the already log10-transformed values into log10 based values again, is there a way indicate that I want my y-axis tick values to be 101, 102, 103, etc. (ie: the the plotted values are log10 transformed, but the y-ticks indicate the actual values before the log10 transformation? Or am I approaching this problem incorrectly?

1
Are you looking for scales::math_format(format = log10)? - teunbrand

1 Answers

1
votes

You can just use math_format() with its default arguments:

test_plt <- ggplot(data = test_df,aes(x = xval,y = yval)) +
    geom_point() + 
    scale_y_continuous(breaks = seq(1,3,by = 1),
                       limits = c(1,3),
                       labels = math_format())

print(test_plt)

enter image description here

From help("math_format"):

Usage
... [Some content omitted]...

math_format(expr = 10^.x, format = force)

This is exactly the formatting you want, 10^.x, rather than 10^.x after a log10 transformation, which is what you get when you call it within trans_format("log10", math_format(10^.x))