我正在构建一个条形图,条形图足以作为水平(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)
)
Run Code Online (Sandbox Code Playgroud)
在这一点上,看起来我将不得不压制所有的网格线,然后用geom_hline()将它们拉回来,这看起来很痛苦(同样,我也不能完全清楚如何找到勾号/主要网格线位置以提供给geom_hline().)
任何想法将不胜感激!
dav*_*net 138
从ggplot2 0.9.2开始,使用"主题" 变得更容易.您现在可以分别将主题分配给panel.grid.major.x和panel.grid.major.y,如下所示.
# simulate data for the bar graph
data <- data.frame( X = c("A","B","C"), Y = c(1:3) )
# make the bar graph
ggplot( data ) +
geom_bar( aes( X, Y ) ) +
theme( # remove the vertical grid lines
panel.grid.major.x = element_blank() ,
# explicitly set the horizontal lines (or they will disappear too)
panel.grid.major.y = element_line( size=.1, color="black" )
)
Run Code Online (Sandbox Code Playgroud)
这个例子的结果看起来很难看,但它演示了如何在保留水平线和x轴刻度线的同时去除垂直线.
lea*_*rnr 16
尝试使用
scale_x_continuous(breaks = NULL)
这将删除所有垂直网格线以及x轴刻度线标签.
小智 7
选项1:
data_df <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))
ggplot(data_df, aes(x, y)) +
geom_bar(stat = 'identity') +
theme(panel.background = element_rect(fill = "white"))
Run Code Online (Sandbox Code Playgroud)
选项2:
data_df <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))
ggplot(data_df, aes(x, y)) +
geom_bar(stat = 'identity') +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank()
)
Run Code Online (Sandbox Code Playgroud)