【问题标题】:How can I suppress the vertical gridlines in a ggplot2 plot?如何抑制 ggplot2 图中的垂直网格线?
【发布时间】:2011-02-10 07:28:47
【问题描述】:

我正在构建一个条形图,其中条形足以指示水平 (x) 位置,因此我想避免绘制多余的垂直网格线。

我了解如何在 opts() 中设置次要和主要网格线的样式,但我终生无法弄清楚如何仅抑制垂直网格线。

library(ggplot2)

data <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))

ggplot(data, aes(x, y)) +
  geom_bar(stat = 'identity') +
  opts(
    panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
    panel.grid.minor = theme_line(colour = NA),
    panel.background = theme_rect(colour = NA),
    axis.ticks = theme_segment(colour = NA)
  )

在这一点上,看起来我将不得不抑制所有网格线,然后使用 geom_hline() 将它们拉回,这似乎有点痛苦(另外,我不完全清楚我怎么能找到要馈送到 geom_hline() 的刻度线/主要网格线位置。)

任何想法将不胜感激!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    从 ggplot2 0.9.2 开始,使用“主题”变得非常更容易做到。您现在可以将主题分别分配给 panel.grid.major.x 和 panel.grid.major.y,如下所示。

    #   simulate data for the bar graph
    data <- data.frame( X = c("A","B","C"), Y = c(1:3) )    
    
    #   make the bar graph
    ggplot( data  ) +
        geom_bar( aes( X, Y ) ) +
        theme( # remove the vertical grid lines
               panel.grid.major.x = element_blank() ,
               # explicitly set the horizontal lines (or they will disappear too)
               panel.grid.major.y = element_line( size=.1, color="black" ) 
        )
    

    这个例子的结果看起来很丑陋,但它演示了如何删除垂直线,同时保留水平线和 x 轴刻度线。

    【讨论】:

    • 也许这需要更新:它给了我Error: stat_count() must not be used with a y aesthetic.
    • 截至 2020 年 6 月 19 日,错误仍然存​​在。此解决方案目前不起作用。
    • @Meg 不确定您是否还在寻找解决方案,但我在这里遇到了 ggExtra 库中的 removeGrid:rdrr.io removeGrid,它在保留轴标签的同时删除了网格线。
    【解决方案2】:

    尝试使用

    scale_x_continuous(breaks = NULL)

    这将删除所有垂直网格线以及 x 轴刻度线标签。

    【讨论】:

    • 做到了,但是现在,正如您所指出的,我没有需要的 x 轴标签 - 关于如何将它们写回的任何想法?
    • 新版本说,Please use breaks = NULL to remove breaks in the scale. (Deprecated; last used in version 0.8.9)
    • scale_x_discrete(breaks = NULL) 是当 x 变量是离散的时使用的。
    • 当我想有标签但没有中断时,这不起作用。
    • @groceryheist 不确定您是否仍在寻找解决方案,但我在这里遇到了 ggExtra 库中的 removeGrid:rdrr.io removeGrid,它在保留轴标签的同时删除了网格线。
    【解决方案3】:

    这让你只剩下数据点:

    ggplot(out, aes(X1, X2)) + 
        geom_point() + 
        scale_x_continuous(breaks = NULL) + 
        scale_y_continuous(breaks = NULL) + 
        opts(panel.background = theme_blank()) + 
        opts(axis.title.x = theme_blank(), axis.title.y = theme_blank())
    

    【讨论】:

    • 也许这需要更新。当我运行它时,我收到以下错误: opts(panel.background = theme_blank()) 中的错误:找不到函数“opts”注意这个答案也不遵循 OP 的可重复答案,它使用“数据”(不是“out”),以及“x”和“y”,而不是“X1”和“X2”。
    【解决方案4】:

    选项 1:

    data_df <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))
    
    ggplot(data_df, aes(x, y)) +
      geom_bar(stat = 'identity') +
      theme(panel.background = element_rect(fill = "white"))
    

    选项 2:

    data_df <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))
        
    ggplot(data_df, aes(x, y)) +
          geom_bar(stat = 'identity') +
          theme(
            panel.grid.major.x = element_blank(),
            panel.grid.minor.x = element_blank(),
            panel.grid.major.y = element_blank(),
            panel.grid.minor.y = element_blank()
          )
    

    【讨论】:

      【解决方案5】:

      从相关帖子复制我的答案,

      对于 2020 年查找此问题的人,我在此处的 ggExtra 库中以 removeGrid 函数的形式找到了解决方案 rdrr.io > removeGrid

      我已经测试它可以与 ggplot2 3.3.0 版和 ggExtra 0.9 版一起使用,给我没有网格线的轴刻度。

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 2022-01-21
        • 1970-01-01
        相关资源
        最近更新 更多