【问题标题】:Overlay grid rather than draw on top of it覆盖网格而不是在其上绘制
【发布时间】:2015-11-29 23:58:02
【问题描述】:

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

请注意,网格或轴线不会通过功能区显示。处理此问题的一种方法是更改​​功能区的alpha 属性;但是,这会使较浅的颜色变得太亮。

另一种想法是将网格/轴线绘制在色带的顶部而不是它们的下方。我怎样才能实现这种渲染顺序?

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

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

【问题讨论】:

  • 如果您根据您的问题制作一个可重复的最小示例,我们更有可能为您提供帮助。我们可以使用的东西向您展示如何解决您的问题。您可以查看this SO post,了解如何在 R 中制作一个出色的可重现示例。
  • @EricFail:由于这通常适用于使用 ggplot 制作的任何图片,因此 MWE 似乎没有必要;但是,我添加了一个生成合适测试用例的 WE。
  • stackoverflow.com/questions/19054092/… 。除非有新的发展,否则额外的 h/vline 几何图形似乎是要走的路
  • 看起来像,有谁知道如何自动获取而不是手​​动输入?
  • 如果 p 是您的绘图(没有 h.vline),您可以通过查看 ggplot_build(p) 获得默认的 x 和 y 网格位置。虽然您可能更喜欢使用 scale_x_continuous 手动定义中断(因此是网格线),并在您的 hline 调用中使用这些相同的值?

标签: r ggplot2


【解决方案1】:

ggplot 在 2015 年 6 月 18 日在 theme() 中引入了一个选项,让您可以使用 pull number 993 做到这一点。

只需添加到您的情节中:

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

ggplotdocs中有一个例子。

【讨论】:

  • 请注意,某些主题(例如,theme_bw() 指定了背景颜色。因此您可能需要在调用中添加另一行,以便您的背景不会遮挡您的图层:@987654327 @
【解决方案2】:

您可以使用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 中自动添加垂直线和水平线(如 Narendra 的回答),但无需手动指定中断,您可以使用 ggplot_build(p) 访问它们的位置,其中 p 是您的情节。


对于带有分面的图表可能值得展示这一点。相同的过程,除了您选择多条线和面板,然后循环它们进行绘制。

# 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])
             }

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

【讨论】:

    【解决方案3】:

    这是使用geom_hline 和geom_vline 的解决方法。

    f <- ggplot(mpg, aes(cty, hwy))
    f + geom_smooth(color="red")
    

    它会生成这个图。

    手动添加水平和垂直线:

    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)
    

    要自动添加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)
    

    现在scale-* 函数引入的axis-ticks 的任何更改都将反映在最终绘图中。
    在这里,我们在图的顶部有水平线和垂直线(类似于网格)。您可以改变size 以使线条更粗。

    但这只是一种解决方法。鉴于ggplot2 包的灵活性,我认为可以使用theme 实现类似的功能。但我不知道怎么做。

    Edit1 : 我们可以尝试跟随,但它不会将网格放在顶部。这样我们就可以更改size、color、linetype,仅此而已。

    f + geom_smooth(color="red") 
      + theme(panel.grid.major=element_line(color="white", size=2))
    

    Edit2 : 添加了使用ggplot_build(f) 自动插入xintercept 和yintercept,如here 所述。

    【讨论】:

    • 谢谢,这帮助很大!但似乎这些天你必须使用ggplot_build(f)$layout$panel_ranges[[1]]$x.major_source
    • @nadieh 你不需要这个,看我的回答
    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    相关资源
    最近更新 更多