Displaying minor logarithmic ticks in x-axis in R

前端 未结 8 481
一个人的身影
一个人的身影 2021-02-02 15:31

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

相关标签:
8条回答
  • 2021-02-02 15:58

    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
    

    0 讨论(0)
  • 2021-02-02 16:00

    Use "" for the labels of the minor ticks.

    0 讨论(0)
  • 2021-02-02 16:07

    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

    0 讨论(0)
  • 2021-02-02 16:09

    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:

    0 讨论(0)
  • 2021-02-02 16:10

    The x axis labels in the first plot on this page is in error. The minor ticks are not properly distributed.

    0 讨论(0)
  • 2021-02-02 16:15

    Try magaxis in package magicaxis.

    0 讨论(0)
提交回复
热议问题