Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R scientific notation in plots

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?

enter image description here

like image 382
Yang Avatar asked Jun 30 '13 03:06

Yang


People also ask

How do I get rid of E+ in R?

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.

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.

How do I get rid of 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 I stop Ggplot from scientific notation?

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() ).


2 Answers

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

enter image description here

like image 183
Mike T Avatar answered Sep 28 '22 13:09

Mike T


This is sort of a hacky way, but there's nothing wrong with it:

plot(x,y/1e3, ylab="y /10^3")
like image 29
David Marx Avatar answered Sep 28 '22 13:09

David Marx