在R中的x轴上显示小对数刻度

发布于 2024-11-28 02:40:45 字数 493 浏览 2 评论 0 原文

我有一个正态分布图和一个直方图,x 轴以对数刻度显示 0、10^0、10^1 ...我想在主要刻度之间包含次要刻度。实际上,我可以使用 上一个问题。我对主要刻度使用了以下代码:

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

可以对其进行编辑以仅标记次要刻度而不对其进行标记吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一城柳絮吹成雪 2024-12-05 02:40:45

Hmisc 包中有一个函数 minor.tick,但该函数对对数标度的处理效果很差。我使用以下函数来获取遵循对数刻度的小刻度。 ax 是您使用它的轴(与函数 axis 相同),n 是小刻度数(默认为 9) , t.ratio 是主要刻度和次要刻度之间的比率,并且使用 ... 您可以将额外的参数传递给 axis

edit :评论中的好主意,所以我编辑了我的函数。有两个额外参数,mnmx,分别表示对数刻度上的最小值和最大值 (mn=0 因此意味着最小值为 10^0 或 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)

给出:

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 : 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 !)

The function:

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)
}

This can be applied as follows :

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)

Gives :

enter image description here

嘿嘿嘿 2024-12-05 02:40:45

这是一个简单的函数:

log10.axis <- function(side, at, ...) {
    at.minor <- log10(outer(1:9, 10^(min(at):max(at))))
    lab <- sapply(at, function(i) as.expression(bquote(10^ .(i))))
    axis(side=side, at=at.minor, labels=NA, tcl=par("tcl")*0.5, ...)
    axis(side=side, at=at, labels=lab, ...)
}

这是一个例子:

x <- exp(rnorm(200, 5))
hist(log(x), 20, xaxt="n", xlim=c(0, 8))
log10.axis(1, at=seq(0, 8, 2))

给出:
输入图像描述这里

Here is a simple function to to this:

log10.axis <- function(side, at, ...) {
    at.minor <- log10(outer(1:9, 10^(min(at):max(at))))
    lab <- sapply(at, function(i) as.expression(bquote(10^ .(i))))
    axis(side=side, at=at.minor, labels=NA, tcl=par("tcl")*0.5, ...)
    axis(side=side, at=at, labels=lab, ...)
}

Here is an example:

x <- exp(rnorm(200, 5))
hist(log(x), 20, xaxt="n", xlim=c(0, 8))
log10.axis(1, at=seq(0, 8, 2))

Gives:
enter image description here

倾其所爱 2024-12-05 02:40:45

尝试magicaxis 包中的magaxis

Try magaxis in package magicaxis.

痞味浪人 2024-12-05 02:40:45

ggplot2中,我们可以使用annotation_logticks< /a> 以及 scales::trans_breaksscales::trans_format。下面是从上面的链接中获取的示例。

    library(ggplot2)

    a <- ggplot(msleep, aes(bodywt, brainwt)) +
      geom_point(na.rm = TRUE) +
      scale_x_log10(
        breaks = scales::trans_breaks("log10", function(x) 10^x),
        labels = scales::trans_format("log10", scales::math_format(10^.x))
      ) +
      scale_y_log10(
        breaks = scales::trans_breaks("log10", function(x) 10^x),
        labels = scales::trans_format("log10", scales::math_format(10^.x))
      ) +
      theme_bw()

    a + annotation_logticks() # Default: log ticks on bottom and left

输入图片此处描述

In ggplot2, we can use annotation_logticks together with scales::trans_breaks and scales::trans_format. Below is an example taken from the link above.

    library(ggplot2)

    a <- ggplot(msleep, aes(bodywt, brainwt)) +
      geom_point(na.rm = TRUE) +
      scale_x_log10(
        breaks = scales::trans_breaks("log10", function(x) 10^x),
        labels = scales::trans_format("log10", scales::math_format(10^.x))
      ) +
      scale_y_log10(
        breaks = scales::trans_breaks("log10", function(x) 10^x),
        labels = scales::trans_format("log10", scales::math_format(10^.x))
      ) +
      theme_bw()

    a + annotation_logticks() # Default: log ticks on bottom and left

enter image description here

草莓酥 2024-12-05 02:40:45

StratigrapheR 包中有一个 minorAxis 函数,可用于任何类型的次要刻度。它可以与 seq_log 函数一起使用来制作对数刻度:

library(StratigrapheR)

x <- exp(rnorm(200, 5))
hist(log10(x), 20, xaxt="n", xlim=c(0, 4), xlab = "x", main = "Logarithmic Histogram of x")

ticks <- seq_log(10^0,10^4, divide = T)

lab <- sapply(0:4, function(i) as.expression(bquote(10^ .(i))))

minorAxis(1, at.maj = log10(ticks[[1]]), at.min = log10(ticks[[2]]), labels = lab)

给出:在此处输入图像描述

There is the minorAxis function in the StratigrapheR package, that can be used for any kind of minor ticks. It can be used with the seq_log function to make logarithmic ticks:

library(StratigrapheR)

x <- exp(rnorm(200, 5))
hist(log10(x), 20, xaxt="n", xlim=c(0, 4), xlab = "x", main = "Logarithmic Histogram of x")

ticks <- seq_log(10^0,10^4, divide = T)

lab <- sapply(0:4, function(i) as.expression(bquote(10^ .(i))))

minorAxis(1, at.maj = log10(ticks[[1]]), at.min = log10(ticks[[2]]), labels = lab)

Gives: enter image description here

囚我心虐我身 2024-12-05 02:40:45

使用 "" 作为小刻度的标签。

Use "" for the labels of the minor ticks.

捎一片雪花 2024-12-05 02:40:45

出现一个小错误,

缺少 lims<-lims[3:4]

   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 <- 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)
}

There was a small error,

lims<-lims[3:4] was missing

   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 <- 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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文