R 更改 y 轴上的刻度格式 [英] R changing format of scale on y-axis

查看:62
本文介绍了R 更改 y 轴上的刻度格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,分别在 y 和 x 轴上有 $-amounts 和日期.目前,美元金额范围为 0-1500 万美元.像这样:

I have a plot that has $-amounts and dates on y and x axis respectively. Currently the dollar amounts range from $0-15 million. Something like this:

x <- rnorm(20)^2 * 1000000
plot(x)

R 执行诸如 '1.0e+07' 而不是 '10,000,000' 之类的东西,并且还将文本垂直而不是水平定向.

R does stuff like '1.0e+07' instead of '10,000,000' and also orients the text vertically instead of horizontally.

我的问题是:

1) 如何让比例文本水平方向?

1) how would I get the scale text to be horizontally oriented?

2) 我如何让 R 使用 10MM 而不是 '10,000,000''1.0e+07'?

2) how would I get R to use 10MM instead of '10,000,000' or '1.0e+07'?

推荐答案

1) 参见 ?options 中的 scipen 选项,这是对使用科学记数法的惩罚.为了更好地控制,您需要使用所需的标签手动绘制轴.

1) See the scipen option in ?options which is a penalty against the use of scientific notation. For better control, you need to plot the axis by hand with labels you want.

2) 参见 ?par 中的 las,它控制粗略的轴标签方向.

2) See las in ?par which controls to orientation crudely of axis labels.

对于 1):

x <- rnorm(20)^2 * 10000000
layout(matrix(1:2, ncol = 2))
plot(x)
getOption("scipen")
opt <- options("scipen" = 20)
getOption("scipen")
plot(x)
options(opt)
layout(1)

给出

要绘制自己的轴尝试

plot(x / 10000000, axes = FALSE)
axis(1)
pts <- pretty(x / 10000000)
axis(2, at = pts, labels = paste(pts, "MM", sep = ""))
box()

哪个给了

我们使用 pretty() 为刻度选择漂亮的位置,就像 R 一样,然后添加自定义轴.请注意我们如何在 plot() 调用中抑制轴绘制,然后通过调用 axis()box()<添加回轴和图框/代码>.

Where we use pretty() to select pretty locations for the ticks just as R would and then add a custom axis. Notice how we suppress axis drawing in the plot() call and then add back the axes and the plot frame with calls to axis() and box().

对于 2) 与 1) 结合

For 2) combining with 1)

opt <- options("scipen" = 20)
op <- par(mar = c(5,7,4,2) + 0.1) ## extra margin to accommodate tick labs
x <- rnorm(20)^2 * 10000000
plot(x, las = 1, ylab = "")       ## no y-axis label 
title(ylab = "label", line = 5.5) ## need to plot the axis label
par(op)
options(opt)

哪个给了

注意我们如何在 plot() 调用中使用 las,我们需要创建一些额外的边距空间来容纳刻度标签.我们还需要手动绘制标签,否则 R 会将其粘贴在刻度标签中.

Notice how we use las in the plot() call, and we need to create some extra margin space to accommodate the tick labels. We also need to plot the label by hand otherwise R will stick it in amongst the tick labels.

对于自定义轴标签,将 las = 1 添加到 axis() 调用:

For the custom axis labels, add the las = 1 to the axis() call:

op <- par(mar = c(5,5,4,2) + 0.1)
plot(x / 10000000, axes = FALSE, ylab = "")
axis(1)
pts <- pretty(x / 10000000)
axis(2, at = pts, labels = paste(pts, "MM", sep = ""), las = 1)
title(ylab = "my label", line = 4)
box()
par(op)

产生什么

这篇关于R 更改 y 轴上的刻度格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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