如何抑制 ggplot2 图中的垂直网格线?

发布于 2024-08-29 18:29:07 字数 616 浏览 2 评论 0 原文

我正在构建一个条形图,其中条形足以作为水平 (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()。)

任何想法将不胜感激!

I am building a bar chart for which bars suffice as indications of horizontal (x) placement, so I'd like to avoid drawing the superfluous vertical gridlines.

I understand how to style the minor and major gridlines in opts(), but I can't for the life of me figure out how to suppress just the vertical gridlines.

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)
  )

At this point, it's looking like I'm going to have to suppress all of the gridlines and then draw them back in with geom_hline(), which seems like kind of a pain (also, it's not entirely clear how I can find the tick/major gridline positions to feed to geom_hline().)

Any thoughts would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽你眉间 2024-09-05 18:29:07

从 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 轴刻度线。

As of ggplot2 0.9.2, this has become much easier to do using "themes." You can now assign themes separately to panel.grid.major.x and panel.grid.major.y, as demonstrated below.

#   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" ) 
    )

The result of this example is quite ugly looking, but it demonstrates how to remove the vertical lines while preserving the horizontal lines and x-axis tick-marks.

捶死心动 2024-09-05 18:29:07

尝试使用

scale_x_连续(中断= NULL)

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

Try using

scale_x_continuous(breaks = NULL)

This would remove all the vertical gridlines as well as x-axis tickmark labels.

泪眸﹌ 2024-09-05 18:29:07

选项 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()
      )

Option 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"))

Option 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()
      )
翻身的咸鱼 2024-09-05 18:29:07

这只剩下数据点:

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())

This leaves you only with the data points:

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())
隐诗 2024-09-05 18:29:07

从相关主题复制我的答案,

对于在 2020 年查找此问题的人,我在这里从 ggExtra 库中找到了以removeGrid 函数形式存在的解决方案 rdrr.io >删除网格

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

Copying my answer from a related thread,

For people looking this up in 2020, I have found a solution in the form of the removeGrid function from the ggExtra library here rdrr.io > removeGrid

I have tested it to be working with ggplot2 version 3.3.0 and ggExtra version 0.9, giving me axis ticks without the gridlines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文