在R的x轴上显示次对数刻度 [英] Displaying minor logarithmic ticks in x-axis in R

查看:180
本文介绍了在R的x轴上显示次对数刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正态分布图和一个带x轴的直方图,对数刻度显示0、10 ^ 0、10 ^ 1 ...我想在主要刻度之间包括次要刻度.实际上,我可以使用

I have a normal distribution plot and a histogram plot with x axis in log scale displaying 0, 10^0, 10^1 ... I want to include minor ticks between the major ones. Actually I was able to change the major ticks format from 1, 2, 3 and so on to 10^0, 10^1, 10^2, 10^3 using the solution given to me in my previous question. I used the following code for the major ticks :

major.ticks <- axTicks(1)
labels <- sapply(major.ticks,function(i)
            as.expression(bquote(10^ .(i)))
          )
axis(1,at=major.ticks,labels=labels)

是否可以对其进行编辑以仅标记较小的刻度线而不标记它们?

Can this be edited to just mark the minor ticks without labeling them?

推荐答案

Hmisc中有一个函数minor.tick,但是该函数在对数刻度方面的处理较差.我使用以下函数来获取跟随对数刻度的小刻度线. ax是您在其上使用的轴(与功能axis相同),n是次刻度的数量(默认为9),t.ratio是主刻度和次刻度之间的比率,并使用...您可以将其他参数传递给axis

There is a function minor.tick in the package Hmisc, but that one deals poorly with logarithmical scales. I use the following function for getting minor ticks that follow the logarithmical scale. ax is the axis you use it on (same as for the function axis), n is the number of minor ticks (default to 9), t.ratio is the ratio between the major and the minor ticks, and with ... you can pass extra parameters to axis

edit:评论中的好主意,所以我编辑了函数.有两个额外的参数,mnmx分别是对数刻度上的最小值和最大值(mn=0表示最小值为10 ^ 0或1!)

edit : Nice idea in the comments, so I edited my function. There are two extra parameters, mn and mx for the minimum and the maximum on the logarithmic scale (mn=0 thus means the minimum is 10^0 or 1 !)

功能:

minor.ticks.axis <- function(ax,n,t.ratio=0.5,mn,mx,...){

  lims <- par("usr")
  if(ax %in%c(1,3)) lims <- lims[1:2] else lims[3:4]

  major.ticks <- pretty(lims,n=5)
  if(missing(mn)) mn <- min(major.ticks)
  if(missing(mx)) mx <- max(major.ticks)

  major.ticks <- major.ticks[major.ticks >= mn & major.ticks <= mx]

  labels <- sapply(major.ticks,function(i)
            as.expression(bquote(10^ .(i)))
          )
  axis(ax,at=major.ticks,labels=labels,...)

  n <- n+2
  minors <- log10(pretty(10^major.ticks[1:2],n))-major.ticks[1]
  minors <- minors[-c(1,n)]

  minor.ticks = c(outer(minors,major.ticks,`+`))
  minor.ticks <- minor.ticks[minor.ticks > mn & minor.ticks < mx]


  axis(ax,at=minor.ticks,tcl=par("tcl")*t.ratio,labels=FALSE)
}

这可以应用如下:

x <- 10^(0:8)
y <- 1:9
plot(log10(x),y,xaxt="n",xlab="x",xlim=c(0,9))
minor.ticks.axis(1,9,mn=0,mx=8)

送礼:

这篇关于在R的x轴上显示次对数刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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