Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force axis values to scientific notation in ggplot [duplicate]

Tags:

I have the following code:

 library(ggplot2)
 df <- data.frame(y=seq(1, 1e5, length.out=100), x=sample(100))
 p  <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
 p

Which produce this image:

enter image description here

As mentioned in the image above how can I change the y-axis value to scientific notation?

like image 823
neversaint Avatar asked Feb 19 '17 03:02

neversaint


People also ask

How do you make Y axis not in scientific notation in R?

First of all, create a vector and its plot using plot function. Then, use options(scipen=999) to remove scientific notation from the plot.

How do you stop scientific notation in ggplot2?

The scale_x_continuous() and scale_y_continuous() methods can be used to disable scientific notation and convert scientific labels to discrete form.

How do you do scientific notation in R?

R uses scientific e notation where e tells you to multiple the base number by 10 raised to the power shown. Let's start with the number 28. Scientific notation adds a decimal after the first number before applying the system. So 28 becomes 2.8 x 10^1 or 2.8e+01 in e notation.

What is Scipen in R?

scipen : integer. A penalty to be applied when deciding to print numeric values in fixed or exponential notation. Positive values bias towards fixed and negative towards scientific notation: fixed notation will be preferred unless it is more than scipen digits wider.


1 Answers

You can pass a format function with scientific notation turned on to scale_y_continuous labels parameter:

p + scale_y_continuous(labels = function(x) format(x, scientific = TRUE))

enter image description here

like image 192
Psidom Avatar answered Oct 16 '22 06:10

Psidom