如何在保留 x 轴标签的同时抑制 ggplot2 图中的垂直网格线?

发布于 2024-08-30 05:59:50 字数 1413 浏览 1 评论 0原文

这是 这个问题的后续内容,我试图抑制垂直网格线

learnr 提供的解决方案是添加scale_x_continuous(breaks = NA),但这也会产生抑制 x 轴标签的副作用。我非常高兴用手写回标签,但我不清楚如何弄清楚标签应该放在哪里。

另一个选项是抑制所有网格线(使用 opts( panel.grid.major = theme_blank()) 或类似的方法),然后仅绘制主要水平网格线。同样,这里的问题是如何找出图中的中断点以提供给 geom_hline()。

所以,本质上,我的选择是:

  1. 抑制垂直网格线和 x 轴标签(使用 scale_x_continuous(breaks = NA) )并将 x 轴标签添加回来。
  2. 抑制所有网格线(使用 opts( panel.grid.major = theme_blank( )) ) 并使用 geom_hline() 添加主要水平网格线。

这里有两个选项:

library(ggplot2)

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

# suppressing vertical gridlines and x-axis labels
# need to re-draw x-axis labels
ggplot(data, aes(x, y)) +
  geom_bar(stat = 'identity') +
  scale_x_continuous(breaks = NA) +
  opts(
    panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
    panel.grid.minor = theme_blank(),
    panel.background = theme_blank(),
    axis.ticks = theme_blank()
  )

# suppressing all gridlines
# need to re-draw horizontal gridlines, probably with geom_hbar() 
ggplot(data, aes(x, y)) +
  geom_bar(stat = 'identity') +
  scale_x_continuous(breaks = NA) +
  opts(
    panel.grid.major = theme_blank(),
    panel.grid.minor = theme_blank(),
    panel.background = theme_blank(),
    axis.ticks = theme_blank()
  )

This is a follow-on from this question, in which I was trying to suppress the vertical gridlines.

The solution, as provided by learnr, was to add scale_x_continuous(breaks = NA), but this had the side effect of also suppressing the x-axis labels, as well. I am totally happy to write the labels back in by hand, but it's not clear to me how to figure out where the labels should go.

The other option is to suppress all gridlines (using opts( panel.grid.major = theme_blank()) or some such) and then drawing back in just the major horizontal gridlines. Again, the problem here is how to figure out what the breaks are in the plot to supply to geom_hline().

So, essentially, my options are:

  1. Suppress vertical gridlines and x-axis labels (using scale_x_continuous(breaks = NA) ) and add the x-axis labels back in.
  2. Suppress all gridlines (using opts( panel.grid.major = theme_blank()) ) and add the major horizontal gridlines back in using geom_hline().

Here are the two options:

library(ggplot2)

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

# suppressing vertical gridlines and x-axis labels
# need to re-draw x-axis labels
ggplot(data, aes(x, y)) +
  geom_bar(stat = 'identity') +
  scale_x_continuous(breaks = NA) +
  opts(
    panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
    panel.grid.minor = theme_blank(),
    panel.background = theme_blank(),
    axis.ticks = theme_blank()
  )

# suppressing all gridlines
# need to re-draw horizontal gridlines, probably with geom_hbar() 
ggplot(data, aes(x, y)) +
  geom_bar(stat = 'identity') +
  scale_x_continuous(breaks = NA) +
  opts(
    panel.grid.major = theme_blank(),
    panel.grid.minor = theme_blank(),
    panel.background = theme_blank(),
    axis.ticks = theme_blank()
  )

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

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

发布评论

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

评论(5

等往事风中吹 2024-09-06 05:59:50

由于注释中的代码不能很好地显示,所以我将其发布为答案。您可以执行类似的操作并使用 geom_text() 手动添加标签:

ggplot(data, aes(x, y)) +
        geom_bar(stat = 'identity') +
        scale_x_continuous(breaks = NA) +
        opts(
                panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
                panel.grid.minor = theme_blank(),
                panel.background = theme_blank(),
                axis.ticks = theme_blank()
        )+
        geom_text(aes(label = x, y = -.3))

As code in comments does not display nicely, so I am posting this as an answer. You could do something like this and add labels manually with geom_text():

ggplot(data, aes(x, y)) +
        geom_bar(stat = 'identity') +
        scale_x_continuous(breaks = NA) +
        opts(
                panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
                panel.grid.minor = theme_blank(),
                panel.background = theme_blank(),
                axis.ticks = theme_blank()
        )+
        geom_text(aes(label = x, y = -.3))
三寸金莲 2024-09-06 05:59:50

上面的答案在 ggplot2 版本 0.9.2.1 及更高版本中不起作用。幸运的是,现在有一种更简单的方法可以做到这一点,正如在回答另一个问题时所描述的那样:https://stackoverflow.com/a /8992102/800044

The answers above will not work in ggplot2 version 0.9.2.1 and above. Fortunately, there is now an easier way to do this, as described in response to a different question: https://stackoverflow.com/a/8992102/800044.

巷雨优美回忆 2024-09-06 05:59:50

您可以直接编辑grob,尝试:

grid.remove(gPath("panel.grid.minor.x.polyline"),grep=T)
grid.remove(gPath("panel.grid.major.x.polyline"),grep=T) 

它会去除您的垂直线。我只是在函数内使用它时遇到问题,因为我猜它仅在打印 ggplot 时才有效。

但是,如果这不是您的情况并且您只需要图形,那么它就可以了。

You can do this editing the grob directly, try:

grid.remove(gPath("panel.grid.minor.x.polyline"),grep=T)
grid.remove(gPath("panel.grid.major.x.polyline"),grep=T) 

It will strip off your vertical lines. I'm just having problems to use it inside a function, because I guess that it only works when the ggplot is printed.

But, if that's not your case and you'll just need the graphic, than it will work.

甜味超标? 2024-09-06 05:59:50

好吧,我在谷歌搜索这个问题时找到了这个解决方案。我还没有尝试过。

http://wiki.stdout.org/rcookbook/Graphs/Axes%20 (ggplot2)/

你必须向下滚动一点。

最好的,

菲利克斯

Well I found this solution while googling for this problem. I have not tried it, yet.

http://wiki.stdout.org/rcookbook/Graphs/Axes%20(ggplot2)/

You have to scroll down a bit.

Best,

Felix

疯了 2024-09-06 05:59:50

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

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

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 和您的相关数据。
原文