我有一个ggplot2图表,如下所示:
请注意,网格或轴线不会通过色带显示.解决这个问题的一种方法是改变alpha色带的性质; 但是,这可以使较浅的颜色太亮.
另一种想法是在丝带顶部而不是在丝带下方绘制网格/轴线.如何实现此渲染排序?
当然,可以询问ggplot2生成的任何图表的问题.但是说明问题的复制粘贴命令如下:
ggplot(data.frame(x=sample(1:100),y=sample(1:100)),aes(x=x,y=y))+geom_point(size=20)
Run Code Online (Sandbox Code Playgroud)
ggplot推出了一个选项theme(),让你可以在2015年6月18日以993的拉号进行操作.
只需添加到您的情节:
+ theme(
panel.background = element_rect(fill = NA),
panel.ontop = TRUE
)
Run Code Online (Sandbox Code Playgroud)
ggplot 文档中有一个例子.
这是使用geom_hlineand的解决方法geom_vline。
f <- ggplot(mpg, aes(cty, hwy))
f + geom_smooth(color="red")
Run Code Online (Sandbox Code Playgroud)
它生成这个图。
手动添加水平线和垂直线:
f + geom_smooth(color="red")
+ geom_vline(xintercept = c(10,15,20,25,30,35), color="white", size=1.25)
+ geom_hline(yintercept = c(20,30,40), color="white", size=1.25)
Run Code Online (Sandbox Code Playgroud)
自动添加xintercept和yintercept:
f <- ggplot(mpg, aes(cty, hwy)) + geom_smooth(color="red")
x_intercept <- ggplot_build(f)$panel$ranges[[1]]$x.major_source
## x_intercept
## [1] 10 15 20 25 30 35
y_intercept <- ggplot_build(f)$panel$ranges[[1]]$y.major_source
## y_intercept
## [1] 20 30 40
f + geom_vline(xintercept=x_intercept, color="white", size=1.25)
+ geom_hline(yintercept=y_intercept, color="white", size=1.25)
Run Code Online (Sandbox Code Playgroud)
现在axis-ticks,scale-*函数引入的任何更改都将反映在最终图中。
这里我们在图的顶部有水平线和垂直线(类似于网格)。您可以更改size以使线条更粗。

但这只是一种解决方法。鉴于ggplot2包的灵活性,我认为可以使用theme. 但是我不知道怎么做。
Edit1 : 我们可以尝试跟随,但它不会将网格放在顶部。这样我们就可以改变size, color,linetype但仅此而已。
f + geom_smooth(color="red")
+ theme(panel.grid.major=element_line(color="white", size=2))
Run Code Online (Sandbox Code Playgroud)
EDIT2:新增的自动插入xintercept和yintercept使用ggplot_build(f)作为解释在这里。
您可以使用grid包功能从绘图中提取网格线,然后重绘它们,这样可以避免添加水平或垂直线时的某些手动指定.
library(ggplot2)
library(grid)
# Draw your plot
ggplot(data.frame(x=sample(1:100),y=sample(1:100)), aes(x=x,y=y))+
geom_point(size=20)
# This extracts the panel including major and minor gridlines
lines <- grid.get("grill.gTree", grep=TRUE)
# Redraw plot without the gridlines
# This is done, as otherwise when the lines are added again they look thicker
last_plot() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank())
# Navigate to relevant viewport
# To see these use grid.ls(viewports=TRUE)
seekViewport("panel.3-4-3-4")
# Redraw lines
grid.draw(lines$children[-1])
Run Code Online (Sandbox Code Playgroud)
哪个产生
或者,如果你想在ggplot中自动添加垂直和水平线(如Narendra的答案),但没有手动指定断点,你可以使用ggplot_build(p),p你的情节在哪里访问它们的位置.
对于带有facets的图表,可能值得展示这一点.相同的过程,除了您选择多行和面板,然后只是循环它们绘制.
# New plot with facets
ggplot(mtcars, aes(mpg, wt)) + geom_point(size=10) + facet_grid(am~cyl)
gr <- grid.ls(print=FALSE)
# Get the gTree for each of the panels, as before
lines <- lapply(gr$name[grep("grill.gTree", gr$name)], grid.get)
last_plot() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank())
# Get the names from each of the panels
panels <- gr$name[grep("panel.\\d", gr$name)]
# Loop through the panels redrawing the gridlines
for(i in 1:length(panels)) {
seekViewport(panels[i])
grid.draw(lines[[i]]$children[-1])
}
Run Code Online (Sandbox Code Playgroud)
这也适用于没有事实的情节.