【问题标题】:ggplot2: Have shorter tick marks for tick marks without labelsggplot2:对没有标签的刻度线有更短的刻度线
【发布时间】:2015-01-08 21:50:18
【问题描述】:

我有一个使用 ggplot2 的绘图,其中我想要沿 x 轴的许多刻度,但只有一些刻度会有与之关联的刻度标签。但是,我希望那些有标签的刻度线比那些没有标签的要长。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    在基础 R 中,您可以使用初始 plot 调用(使用 xaxt='n')来抑制 x 轴,然后使用 tcl 来控制刻度长度(请参阅 ?par)和两个 axis(1, ...) 调用.

    plot(1:10, xaxt='n', ylab='', las=1)
    axis(1, at=1:10, tcl=-0.8)
    axis(1, at=seq(0, 11, 0.2), labels=NA)
    

    【讨论】:

    • 不错,但 OP 要求提供 ggplot,而不是基本 R 图形
    • @smci 没关系,还是很棒的。
    • @scmi - 我在发布后看到了标签(最好将您的关键要求包含在帖子中)。无论如何,在这里有一个基本的 R 解决方案并没有什么坏处,因为想要使用基本的人可能会偶然发现这个问题。
    • 这个答案很棒,但总的来说,使用 R 基础图形正在走上一条非常快速地限制你的道路,并导致难以辨认的粗劣不可扩展不可重用代码。最终你把它扔掉并切换到 ggplot (或其他库)。除非它是针对 ggplot 不能很好地服务的特定绘图类型。
    • 公平点。虽然我发现在某些情况下,使用基本图形进行绘图比 ggplot/lattice 等价物要简单得多,但这可能是因为我没有花足够的时间来学习后者(它在我的待办事项清单上)。与 R 一样,有很多方法可以给猫剥皮,IMO 很高兴 SO 可以为问题提供替代解决方案。 :)
    【解决方案2】:

    使用基本图形更容易,但这里是使用 ggplot2 的尝试。

    该方法基于this method,它将空白标签插入标签序列(使用该答案中的代码)。一旦中断和标签在 ggplot 中就位,我钻入绘图结构以编辑刻度线的长度。 (小心使用;打破它并不难。)

    library(ggplot2)
    library(grid)
    
    # Data
     df = data.frame(x = 1:10, y = 1:10)
    
    # Range of x values
    range = 1:10
    
    # Major tick marks
    major = 1
    
    # Minor tick marks
    minor = 0.2
    
    # Function to insert blank labels
    # Borrowed from https://stackoverflow.com/questions/14490071/adding-minor-tick-marks-to-the-x-axis-in-ggplot2-with-no-labels/14490652#14490652
    insert_minor <- function(major, n_minor) {
          labs <- c(sapply(major, function(x, y) c(x, rep("", y) ), y = round(n_minor)))
          labs[1:(length(labs) - n_minor)]
    }
    
    # Getting the 'breaks' and 'labels' for the ggplot
    n_minor = major/minor - 1
    breaks = seq(min(range), max(range), minor)
    labels = insert_minor(seq(min(range), max(range), major), n_minor)
    if(length(breaks) > length(labels)) labels = c(labels, rep("", length(breaks) - length(labels)))
    
    # The plot
    p <- ggplot(df, aes(x = x, y = y)) + 
         geom_point() + 
         scale_x_continuous(breaks = breaks, labels = labels) + 
         coord_cartesian(xlim = range) +
         theme_bw() +
         theme(panel.grid = element_blank(),
               axis.text.x = element_text(margin = margin(t = 5, unit = "pt")))
    p
    
    # Edit the plot:
    # Change the lengths of the major tick marks
    
    g = ggplotGrob(p)
    
    # Get the x axis
    xaxis <- g$grobs[[which(g$layout$name == "axis-b")]]  
    
    # Get the tick marks and tick mark labels   
    ticks <- xaxis$children[[2]]
    
    # Get the tick marks
    marks = ticks$grobs[[1]]
    
    # Edit the y positions of the end points of the tick marks
    # The '6' and the '3' in the code below 
    # are the lengths in pts of the major and minor tick marks respectively. 
    marks$y = unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), unit(1, "npc"),   
                     rep(unit.c(unit(1, "npc") - unit(3, "pt"), unit(1, "npc")), n_minor)))
    
    # Put the tick marks back into the plot
    ticks$grobs[[1]] = marks
    xaxis$children[[2]] = ticks
    g$grobs[[which(g$layout$name == "axis-b")]]  = xaxis
    
    # Draw the plot
    grid.newpage()
    grid.draw(g)
    

    【讨论】:

    • 这对我有用。除了我将您重置标记的行更改为以下内容:marks$y[seq(1,length(marks$y),2)][-seq(1,length(marks$y)/2, n_minor+1)] &lt;- unit(1,'npc') - unit(3,'pt') 使用这种方式,您还应该在主题中设置axis.ticks.length = unit(6, "pt")。
    • 注意:我们已将此解决方案用于平面网格图,请参阅:stackoverflow.com/q/53660703/6574038
    • 四年后 ggplot2 主题没有更优雅的解决方案是荒谬的!虽然经过大量搜索,ggh4x package 提供了一个解决方案
    【解决方案3】:

    theme(axis.ticks.length)

    http://docs.ggplot2.org/0.9.2.1/theme.html 应该很有用。

    编辑抱歉,请仔细阅读您的问题。标记的是标准的major.grid 刻度吗?或以某种自定义方式标记?

    【讨论】:

    • 它们是major.grid刻度,使用theme(axis.ticks.length)将定义所有刻度的长度,而不仅仅是带有标签的刻度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多