Logarithmic y-axis Tick Marks in R plot() or ggplot2()

后端 未结 4 645
梦谈多话
梦谈多话 2021-02-03 10:14

I saw the ideal tick-mark structure for a log=\"y\" plot in this paper, Figure 3b 3c 3d.

It has short, log-spaced minor tick marks without labels, plus long

4条回答
  •  后悔当初
    2021-02-03 11:14

    Here is a ggplot2 solution:

    library(ggplot2)
    
    set.seed(20180407)
    
    df = data.frame(x = seq(from = 1, by = 1, length.out = 20),
                    y = 2^(seq(to = 1, by = -1, length.out = 20) + rnorm(20, 0, 0.7)))
    
    ggplot(data = df, aes(x = x, y = y)) +
      geom_line() +
      scale_y_log10() +
      annotation_logticks(sides = "l") 
    

    You can make it look even more than the paper you linked to with some theming:

    ggplot(data = df, aes(x = x, y = y)) +
      geom_line(colour = "blue") +
      geom_point(colour = "blue") +
      scale_y_log10() +
      annotation_logticks(sides = "l") +
      theme_minimal() +
      theme(panel.grid = element_blank(), 
            axis.line = element_line(),
            axis.ticks.x = element_line())
    

提交回复
热议问题