如何抑制 ggplot2 图中的垂直网格线? [英] How can I suppress the vertical gridlines in a ggplot2 plot?
问题描述
我正在构建一个条形图,其中的条形足以指示水平 (x) 位置,因此我想避免绘制多余的垂直网格线.
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.
我了解如何在 opts() 中设置次要和主要网格线的样式,但我终生无法弄清楚如何仅抑制垂直网格线.
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)
)
在这一点上,看起来我将不得不抑制所有网格线,然后使用 geom_hline() 将它们重新绘制,这似乎有点痛苦(而且,我也不完全清楚如何才能找到要提供给 geom_hline() 的刻度线/主要网格线位置.
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().)
任何想法将不胜感激!
推荐答案
尝试使用
scale_x_continuous(breaks = NULL)
scale_x_continuous(breaks = NULL)
这将删除所有垂直网格线以及 x 轴刻度线标签.
This would remove all the vertical gridlines as well as x-axis tickmark labels.
这篇关于如何抑制 ggplot2 图中的垂直网格线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!