R scientific notation on sites

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 it possible to use scientific notation for the y axis and put * 10^3 on the top of the axis, as shown in the figure below?

enter image description here

+6
source share
3 answers

This is a kind of hacker way, but there is nothing wrong with that:

 plot(x,y/1e3, ylab="y /10^3") 
+3
source

A similar method is to use eaxis (extended / engineering axis) from the sfsmisc package.

It works as follows:

 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

+9
source

How you get the labels on your axis depends on the build system you are using (base, ggplot2 or grid). You can use the functions from the scales package to format the 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 is 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 definite need. Here are some ideas I used here for this:

  • You can adjust the graph labels using the axis function.
  • Use mtext to place text in the outer portion of the area.
  • Use an expression to profit from plotmath properties ...

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)) 
+2
source

Source: https://habr.com/ru/post/948408/


All Articles