R scientific notation in plots
Asked Answered
C

3

8

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

Contusion answered 30/6, 2013 at 3:35 Comment(0)
Y
3

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

plot(x,y/1e3, ylab="y /10^3")
Yard answered 30/6, 2013 at 4:7 Comment(1)
Thanks. It would be better using expressions #4302867Contusion
D
13

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

Dneprodzerzhinsk answered 23/1, 2014 at 3:5 Comment(0)
Y
3

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

plot(x,y/1e3, ylab="y /10^3")
Yard answered 30/6, 2013 at 4:7 Comment(1)
Thanks. It would be better using expressions #4302867Contusion
P
3

How you get the labels onto your axis depends upon the used plotting system.(base, ggplot2 or lattice) You can use functions from scales package to format your axis numbers:

library(scales)
x <- 10 ^ (1:10)
scientific_format(1)(x)
[1] "1e+01" "1e+02" "1e+03" "1e+04" "1e+05" "1e+06" "1e+07" "1e+08" "1e+09" "1e+10"

Here an example using ggplot2 :

library(ggplot2)
dat <- data.frame(x  = c(102, 258, 2314),                                                                     
                  y  = c(482563, 922167, 4462665))

qplot(data=dat,x=x,y=y) + 
  scale_y_continuous(label=scientific_format(digits=1))+ 
  theme(axis.text.y =element_text(size=50))

enter image description here

EDIT The OP has a specific need. Here some ideas I used here in order to accomplish this :

  1. You can customize your plot labels using axis function.
  2. Use mtext to put text in the outer plot region
  3. Use expression to profit from the plotmath features...

enter image description here

y <- c(102, 258, 2314)                                                                         
x <- c(482563, 922167, 4462665)
plot(x,y,ylab='',yaxt='n')
mtext(expression(10^3),adj=0,padj=-1,outer=FALSE)
axis(side=2,at=y,labels=round(y/1000,2))
Pedaiah answered 30/6, 2013 at 4:9 Comment(3)
I guess you misunderstood my question; I want 1, 2, 3, 4 with y axis and e+06 to be on the top of the y axis.Contusion
No I understand perfectly your need. My answer was general to invite you to discover R plots features and not to try to reproduce some MATLAB features. Many default R parameters are optimized to easily extract the information from a plot. e.g putting the 10^3, make the plot harder to understand , we are obliged to rescale each time we follow a point...Pedaiah
I understand that, I'm going to put this into a paper and saving space is my priority here.Contusion

© 2022 - 2024 — McMap. All rights reserved.