如何在保留x轴标签的同时抑制ggplot2图中的垂直网格线? [英] How can I suppress the vertical gridlines in a ggplot2 plot while retaining the x-axis labels?

查看:572
本文介绍了如何在保留x轴标签的同时抑制ggplot2图中的垂直网格线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案由学员提供,将scale_x_continuous(breaks = NA) ,但是这也具有抑制x轴标签的副作用。我很高兴将这些标签贴在手上,但我不清楚如何确定标签的位置。

另外一种选择是压制标签所有网格线(使用opts(panel.grid.major = theme_blank())或一些这样的),然后只在主要的水平网格线中拉回。再次,这里的问题是如何计算plot中提供给geom_hline()的断点。



所以,基本上,我的选项是:


  1. 抑制垂直网格线和x轴标签(使用scale_x_continuous(breaks = NA)),并重新添加x轴标签。

  2. 取消所有网格线(使用opts(panel.grid.major = theme_blank())),然后使用geom_hline()重新添加主要水平网格线。


  3. 以下是两个选项:

      library(ggplot2)

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

    #抑制垂直网格线和x轴标签
    #需要重新绘制x轴标签
    ggplot(data,aes(x,y))+
    geom_bar(stat ='identity' )+
    scale_x_continuous(休息= NA)+
    opts(
    panel.grid.major = theme_line(size = 0.5,color ='#1391FF'),
    panel.grid .minor = theme_blank(),
    panel.background = theme_blank(),
    axis.ticks = theme_blank()


    #压缩所有网格线
    #需要重新绘制水平网格线,可能使用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()

    $ / code>


    解决方案

    ,所以我发布这个答案。你可以做这样的事情,用 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,color ='#1391FF'),
    panel.grid.minor = theme_blank(),
    panel.background = theme_blank(),
    轴。 ticks = theme_blank()
    )+
    geom_text(aes(label = x,y = -.3))


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

    解决方案

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

    这篇关于如何在保留x轴标签的同时抑制ggplot2图中的垂直网格线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆