我正在构建一个条形图,其中条形足以作为水平 (x) 放置的指示,因此我想避免绘制多余的垂直网格线。
我了解如何在 opts() 中设置次要和主要网格线的样式,但我无法弄清楚如何仅抑制垂直网格线。
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()。)
任何想法将不胜感激!
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.
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)
)
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().)
Any thoughts would be appreciated!
发布评论
评论(5)
从 ggplot2 0.9.2 开始,使用“主题”变得更更容易做到。现在,您可以将主题分别分配给 panel.grid.major.x 和 panel.grid.major.y,如下所示。
这个示例的结果看起来相当难看,但它演示了如何删除垂直线,同时保留水平线和 x 轴刻度线。
As of ggplot2 0.9.2, this has become much easier to do using "themes." You can now assign themes separately to panel.grid.major.x and panel.grid.major.y, as demonstrated below.
The result of this example is quite ugly looking, but it demonstrates how to remove the vertical lines while preserving the horizontal lines and x-axis tick-marks.
尝试使用
这将删除所有垂直网格线以及 x 轴刻度线标签。
Try using
This would remove all the vertical gridlines as well as x-axis tickmark labels.
选项 1:
选项 2:
Option 1:
Option 2:
这只剩下数据点:
This leaves you only with the data points:
从相关主题复制我的答案,
Copying my answer from a related thread,