R 覆盖网格而不是在其上绘制

R 覆盖网格而不是在其上绘制,r,ggplot2,R,Ggplot2,我有一个ggplot2图,如下所示: ggplot(data.frame(x=sample(1:100),y=sample(1:100)),aes(x=x,y=y))+geom_point(size=20) 请注意,栅格线或轴线不会通过功能区显示。一种处理方法是改变色带的alpha属性;然而,这会使较浅的颜色太浅 另一种方法是在色带顶部而不是下方绘制网格/轴线。如何实现此渲染顺序 当然,这个问题可以问ggplot2生成的任何绘图。但说明问题的复制粘贴命令如下所示: ggplot(data

我有一个ggplot2图,如下所示:

 ggplot(data.frame(x=sample(1:100),y=sample(1:100)),aes(x=x,y=y))+geom_point(size=20)

请注意,栅格线或轴线不会通过功能区显示。一种处理方法是改变色带的
alpha
属性;然而,这会使较浅的颜色太浅

另一种方法是在色带顶部而不是下方绘制网格/轴线。如何实现此渲染顺序

当然,这个问题可以问ggplot2生成的任何绘图。但说明问题的复制粘贴命令如下所示:

 ggplot(data.frame(x=sample(1:100),y=sample(1:100)),aes(x=x,y=y))+geom_point(size=20)

这里有一个使用
geom\u hline
geom\u vline
的解决方法

f <- ggplot(mpg, aes(cty, hwy))
f + geom_smooth(color="red")
要自动添加
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)

Edit2:添加了使用
ggplot\u build(f)
自动插入
xintercept
yintercept

您可以使用
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])

这也适用于没有事实的情节。

ggplot在
theme()
中引入了一个选项,您可以在2015年6月18日使用该选项

只需将以下内容添加到绘图中:

+ theme(
  panel.background = element_rect(fill = NA),
  panel.ontop = TRUE
)

ggplot

中有一个例子,如果您在回答问题时给出一个最小的可重复的例子,我们更有可能帮助您。我们可以利用它来向您展示如何解决您的问题。您可以看看如何在R中创建一个伟大的可复制示例。@EricFail:因为这通常适用于使用ggplot制作的任何图片,所以MWE似乎没有必要;但是,我添加了一个WE,它生成了一个合适的测试用例。除非有新的开发,否则额外的h/vline几何图形似乎是进入类似Golook的方式,有人知道如何自动获取这些几何图形而不是手动输入它们吗?如果p是您的绘图(没有h.vline),您可以通过查看
ggplot\u build(p)
,获得默认的x和y网格位置。尽管您可能更喜欢使用scale_x_continuous手动定义中断(因此网格线),并在您的hline呼叫中使用这些相同的值,但这非常有帮助!但是现在你似乎不得不使用
ggplot\u build(f)$layout$panel\u ranges[[1]]$x.major\u source
@nadieh你不需要这个,请看我的回答,只是注意一些主题(例如,
theme\u bw()
指定背景颜色。因此,您可能需要在调用中添加另一行,以便背景不会模糊图层:
主题(panel.ontop=TRUE,panel.background=element\u rect(color=NA,fill=NA))
# 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])
             }
+ theme(
  panel.background = element_rect(fill = NA),
  panel.ontop = TRUE
)