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:
As mentioned in the image above how can I change the y-axis value to scientific notation?
First of all, create a vector and its plot using plot function. Then, use options(scipen=999) to remove scientific notation from the plot.
The scale_x_continuous() and scale_y_continuous() methods can be used to disable scientific notation and convert scientific labels to discrete form.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With