【问题标题】:How to remove gridlines at specific values using ggplot2?如何使用ggplot2删除特定值的网格线?
【发布时间】:2021-06-07 20:06:58
【问题描述】:

我有一个图表,需要扩展 x 轴刻度以容纳超过我的最大值的标签(1 作为 x 轴代表百分比)。

我可以使用scale_x_continuous 中的limits 参数来做到这一点,但我仍然可以在 1.1 中看到相关的网格线。有没有什么办法可以只删除这条网格线(下图中红色框为边界),但保留展开的图?

library(dplyr)
library(ggplot2)

data.frame(val1 = seq(0, 1, 0.1),
           val2 = seq(0, 1, 0.1)) %>% 
  ggplot(aes(x = val1, y = val2)) +
  scale_x_continuous(limits = c(0, 1.05),
                     breaks = seq(0, 1, 0.1))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这也可以通过设置限制次要中断的范围来实现:

    library(dplyr)
    library(ggplot2)
    
    data.frame(val1 = seq(0, 1, 0.1),
               val2 = seq(0, 1, 0.1)) %>% 
      ggplot(aes(x = val1, y = val2)) +
      scale_x_continuous(limits = c(0, 1.05),
                         breaks = seq(0, 1, 0.1),
                         minor_breaks = seq(0, 1, 0.05))
    

    由reprex package (v2.0.0) 于 2021-06-07 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多