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

前端 未结 5 1121
轮回少年
轮回少年 2021-02-19 23:54

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

相关标签:
5条回答
  • 2021-02-20 00:03

    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

    0 讨论(0)
  • 2021-02-20 00:04

    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.

    0 讨论(0)
  • 2021-02-20 00:11

    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.

    0 讨论(0)
  • 2021-02-20 00:19

    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.

    0 讨论(0)
  • 2021-02-20 00:29

    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))
    
    0 讨论(0)
提交回复
热议问题