简体   繁体   English

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

[英]How can I suppress the vertical gridlines in a ggplot2 plot while retaining the x-axis labels?

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.学习者提供的解决方案是添加 scale_x_continuous(breaks = NA),但这也有抑制 x 轴标签的副作用。 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.另一种选择是抑制所有网格线(使用 opts( panel.grid.major = theme_blank()) 或一些类似的),然后只绘制主要的水平网格线。 Again, the problem here is how to figure out what the breaks are in the plot to supply to geom_hline().同样,这里的问题是如何找出图中的中断点以提供给 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.抑制垂直网格线和 x 轴标签(使用 scale_x_continuous(breaks = NA) )并重新添加 x 轴标签。
  2. Suppress all gridlines (using opts( panel.grid.major = theme_blank()) ) and add the major horizontal gridlines back in using geom_hline().抑制所有网格线(使用 opts( panel.grid.major = theme_blank()) )并使用 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()
  )

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() :您可以执行以下操作并使用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))

The answers above will not work in ggplot2 version 0.9.2.1 and above.上面的答案在 ggplot2 0.9.2.1 及更高版本中不起作用。 Fortunately, there is now an easier way to do this, as described in response to a different question: https://stackoverflow.com/a/8992102/800044 .幸运的是,现在有一种更简单的方法可以做到这一点,如对另一个问题的回答所述: https : //stackoverflow.com/a/8992102/800044

You can do this editing the grob directly, try:您可以直接编辑grob ,尝试:

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.我只是在函数内部使用它时遇到问题,因为我猜它只有在打印 ggplot 时才有效。

But, if that's not your case and you'll just need the graphic, than it will work.但是,如果这不是您的情况并且您只需要图形,那么它就会起作用。

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)/ http://wiki.stdout.org/rcookbook/Graphs/Axes%20(ggplot2)/

You have to scroll down a bit.你必须向下滚动一点。

Best,最好的事物,

Felix菲利克斯

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对于在 2020 年查找此内容的人,我从 ggExtra 库中找到了 removeGrid 函数形式的解决方案,这里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.我已经测试它与 ggplot2 3.3.0 版和 ggExtra 0.9 版一起使用,给我没有网格线的轴刻度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM