迫使R以n.nn x 10 ^ -n标注科学记号 [英] Force R to write scientific notations as n.nn x 10^-n with superscript

查看:177
本文介绍了迫使R以n.nn x 10 ^ -n标注科学记号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个花车

  a <-8.9384920e-24 
b < - 0.00293892837

我想用10位科学计数法显示其中任何一个,可能使用 paste(),但在10之后使用上标格式。

  8.94 x 10 ^ -24 #no ^和上标字体
2.94 x 10 ^ -4 #no ^和上标字体应该是-4,而不是-04

这真的很疯狂,但是已经被上级要求了,它必须在base R(而不是ggplot2)中完成,否则我将不得不重写600行代码...现在我所能看到的是,浮动的打印方式有多大,取决于它们的大小...

解决方案

您可以检查 sfsmisc



<$ eaxis (1e-5,1e-4,len = 10),..., )

#def ault axes
plot(x,y)


 #eaxis 
plot(x,y,axes = FALSE)
eaxis(side = 1)
eaxis(side = 2)



您也可以使用同一包中的 pretty10exp()创建一个标签 expression 。例如,要将格式应用于绘图标题:

  plot(x,y,axes = FALSE)
title (pretty10exp(y [1]))


Let's say I have two floats

a <- 8.9384920e-24
b <- 0.00293892837

I would like to display either of them in 10-base scientific notation rounded to two decimals on a graph, possibly using paste(), but with superscript formatting after the 10.

8.94 x 10^-24 #no ^ and superscript font
2.94 x 10^-4  #no ^ and supercript font, should be -4, not -04

This is really maniac but it has been requested by a superior, it has to be done in base R (not ggplot2) or I will have to re-write 600 lines of code... Right now all I can see is that floats are printed differently depending on how big they are...

解决方案

You may check eaxis in package sfsmisc

# some data
x <- seq(1, 100000, len = 10)
y <- seq(1e-5, 1e-4, len = 10)

# default axes
plot(x, y)

# eaxis
plot(x, y, axes = FALSE)
eaxis(side = 1)
eaxis(side = 2)

You may also create a label expression using pretty10exp() from the same package. For example to apply the format to a plot title:

plot(x, y, axes = FALSE)
title(pretty10exp(y[1]))

这篇关于迫使R以n.nn x 10 ^ -n标注科学记号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆