2

I am not very proficient in R, and I have stumbled upon an obstacle when using ggplot2. I have a plot with large numbers on both the x and y axis of the plot. Since I am dealing with large numbers, ggplot2's default behavior is to label the ticks using "scientific" notation (such as 1e+05, 2e+05, etc.). I would like the tick labels on either axis to be written in "exponential" notation (such as 105, 2·105, etc.). In other words, I would simply like to have the default scientific labels and breaks chosen by ggplot2 rewritten into the exponential notation. I appreciate the suggestions that I have found elsewhere on this site, which offer some solutions for avoiding scientific notation, but to avoid clutter, I specifically ...

  1. do not want to log-rescale the axis with something like:
scale_x_log10(limits = c(1, 1e7), 
breaks = breaks_log(n=8, base = 10),
labels = label_log((base = 10))
  1. do not want to have large numbers written in non-exponential form, which can be achieved with something like this:
scale_x_log10(labels = function(x) format(x, scientific = FALSE))
  1. do not want to use suffixes K, M, G or the like to represent thousands, millions, billions, etc.

The closest thing I have found is posted here: How can I format axis labels with exponents with ggplot2 and scales? by sherifffruitfly. He suggests the following:

scale_y_continuous(label= function(x) {ifelse(x==0, "0", parse(text=gsub("[+]", "", gsub("e", " %*% 10^", scientific_format()(x)))))})

Which is almost what I would want since it gives the numbers in the format 2 x 105, but the labels(numbers) are not horizontally justified, and they do contain an 'x', instead of the '·' between the coefficient and the power of ten. Is there a simple function, for example in scales package, that does what I want? It would also be neat if the solution works with negative numbers with large absolute value, as well as numbers between <-1, 1>, such as -2·105, etc. Thanks everyone for reading, and my apologies if I missed something very obvious.

4
  • 1
    It's easier to help you if you include a simple reproducible example with sample input that can be used to test and verify possible solutions. Commented Jun 21, 2023 at 13:41
  • Suggested minimal reproducible example: df = data.frame(x = (-2:2) * 1e5, y = 1:5) ggplot(df, aes(x, y)) + geom_point() Commented Jun 21, 2023 at 14:10
  • 1
    So, in the suggested solution if you change %*% to %.% you will get a dot instead of the x for multiplication. I'm not sure what you mean by "the numbers are not horizontally justified", but that seems like a separate setting in theme (e.g., theme(axis.text = element_text(hjust = ...))). It seems to work with big negative numbers. That small change seems to meet your requirements. Commented Jun 21, 2023 at 14:21
  • And, to be clear, the code I use was scientific_10 <- function(x) { parse(text=gsub("e", " %.% 10^", scales::scientific_format()(x)))} and then using my sample data in the above comment ggplot(df, aes(x, y)) + geom_point() + scale_x_continuous(labels = scientific_10) Commented Jun 21, 2023 at 15:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.