问题描述
在绘制组织干图,散点图和其他标度为R的对数标度的图时,如何使用诸如10 ^ -1 10 ^ 0 10 ^ 1 10 ^ 2 10 ^ 3之类的标签代替轴仅显示-1、0、1、2、3等.应该在命令中添加哪些参数,例如hist(),plot()等?
While plotting histogarm, scatterplots and other plots with axes scaled to logarithmic scale in R, how is it possible to use labels such as 10^-1 10^0 10^1 10^2 10^3 and so on instead of the axes showing just -1, 0, 1, 2, 3 etc. What parameters should be added to the commands such as hist(), plot() etc?
推荐答案
除了ggplot2的解决方案(请参见gsk3的注释),我想补充一点,当使用正确的参数时,在plot()中也会自动发生这种情况,例如:
Apart from the solution of ggplot2 (see gsk3's comment), I would like to add that this happens automatically in plot() as well when using the correct arguments, eg :
x <- 1:10
y <- exp(1:10)
plot(x,y,log="y")
您可以将参数log="x"
用于X轴,或者将log="xy"
用于两者.
You can use the parameter log="x"
for the X axis, or log="xy"
for both.
如果您要格式化数字或以对数格式存储数据,则可以使用axis()来解决.一些有趣的功能:
If you want to format the numbers, or you have the data in log format, you can do a workaround using axis(). Some interesting functions :
-
axTicks(x)
给出刻度线在X轴(x = 1)或Y轴(x = 2)上的位置 -
bquote()
将表达式转换为语言,但可以用其值替换变量.有关bquote()
的更多信息,请参见问题乳胶和变量在R中的情节标签中?. -
as.expression()
使来自bquote()
的语言对象成为表达式.这使axis()
可以按照?plotmath
中的说明进行格式化.语言对象无法做到这一点.
axTicks(x)
gives you the location of the ticks on the X-axis (x=1) or Y-axis (x=2)bquote()
converts expressions to language, but can replace a variable with its value. More information onbquote()
in the question Latex and variables in plot label in R? .as.expression()
makes the language object coming frombquote()
an expression. This allowsaxis()
to do the formatting as explained in?plotmath
. It can't do so with language objects.
一个很好的格式化示例:
An example for nice formatting :
x <- y <- 1:10
plot(x,y,yaxt="n")
aty <- axTicks(2)
labels <- sapply(aty,function(i)
as.expression(bquote(10^ .(i)))
)
axis(2,at=aty,labels=labels)
哪个给
这篇关于R中的标签对数刻度显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!