I have a simple plot:
#!/usr/bin/Rscript
png('plot.png')
y <- c(102, 258, 2314)
x <- c(482563, 922167, 4462665)
plot(x,y)
dev.off()
R uses 500, 1000, 1500, etc for the y axis. Is there a way I can use scientific notation for the y
axis and put * 10^3
on the top of the axis like the figure below?
You can disable scientific notation in the entire R session by using the scipen option. Global options of your R workspace. Use options(scipen = n) to display numbers in scientific format or fixed. Positive values bias towards fixed and negative towards scientific notation.
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.
First of all, create a vector and its plot using plot function. Then, use options(scipen=999) to remove scientific notation from the plot.
In case you want to omit the use of exponential formatting on one of the axes in a ggplot: Add scale_*_continuous(labels = scales::comma) with * being replaced by the axis you want to change (e.g. scale_x_continuous() ).
A similar technique is to use eaxis
(extended / engineering axis) from the sfsmisc package.
It works like this:
library(sfsmisc)
x <- c(482563, 922167, 4462665)
y <- c(102, 258, 2314)
plot(x, y, xaxt="n", yaxt="n")
eaxis(1) # x-axis
eaxis(2) # y-axis
This is sort of a hacky way, but there's nothing wrong with it:
plot(x,y/1e3, ylab="y /10^3")
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