代码之家  ›  专栏  ›  技术社区  ›  Karl Wolfschtagg

是否有可能只抑制一个勾号标签?

  •  4
  • Karl Wolfschtagg  · 技术社区  · 1 月前

    我正在生成一个图,并试图找出如何抑制一个勾号标签(不是标记,而是标签)。

    以下是一些示例代码:

    x1 <- seq(0, 100, by = 1)
    y1 <- runif(n = 100, min = 1, max = 10)
    library(rando)
    labe <- r_Letters(nchar = 1, n = 100)
    
    test_dat <- as.data.frame(cbind(x1, y1, labe))
    test_dat$x1 <- as.numeric(test_dat$x1)
    test_dat$y1 <- as.numeric(test_dat$y1)
    
    library(ggplot2)
    library(ggrepel)
    g <- ggplot(data = test_dat, aes(x = x1, y = y1))
    g <- g + geom_line()
    g <- g + geom_point()
    g <- g + expand_limits(x = c(-10, 110), y = c(0, 10))
    g <- g + scale_x_continuous(breaks = scales::pretty_breaks(n = 25))
    g <- g + scale_y_continuous(breaks = scales::pretty_breaks(n = 5))
    g <- g + geom_label_repel(data = test_dat, aes(x = x1, y = y1, label = labe), 
                             size = 2.0, color = "blue", box.padding = unit(0.5, "lines"), 
                             point.padding = unit(0, "lines"))
    g
    

    在我的真实数据集中,当我使用geom_label_reject来标记我的点(我真实数据的一个子集;在我的例子中我变得懒惰了)时,我需要扩展图的范围(因此,expand_limits)。所有的勾号都在我想要的地方,但最左边的那个(在我的例子中为-15)是不物理的,我想删除 就这一个。

    我可以想出如何抑制所有的勾号和标签,但这不是我想要的——我想让勾号保持原样,但只需删除最左侧的标签。

    有什么想法吗?

    1 回复  |  直到 1 月前
        1
  •  3
  •   Jon Spring    1 月前

    一种方法是使用一个公式手动调整断点标签,该公式在-15标签处输出一个空格,但在其他情况下则保持不变。

    g <- g + scale_x_continuous(breaks = scales::pretty_breaks(n = 25),
                                labels = ~if_else(.x == -15, "", as.character(.x))) 
    

    enter image description here

        2
  •  0
  •   Carl Witthoft    1 月前

    你为什么设定 expand_limits 首先,如果你不想显示那个区域?我的方法,至少如果你只想在域的一端或另一端抑制标签,一开始就不会绘制该区域。
    此外,我怀疑Ed.Tufte会愤怒地皱眉,如果你想在标签中隐藏一个抽搐标签 中间的 图的域(或范围)。