从ggplot2图表中移除图层 [英] Remove a layer from a ggplot2 chart

查看:183
本文介绍了从ggplot2图表中移除图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从创建的ggplot2中移除图层(在本例中为 geom_ribbon 的结果)网格对象。有一种方法可以在它已经成为对象的一部分时将它移除?

  library(ggplot2)
dat< ; - data.frame(x = 1:3,y = 1:3,ymin = 0:2,ymax = 2:4)
p < - ggplot(dat,aes(x = x,y = y ))+ geom_ribbon(aes(ymin = ymin,ymax = ymax),alpha = 0.3)
+ geom_line()

#这有geom_ribbon
p

#这覆盖了
p + geom_ribbon上的另一个功能区(aes(ymin = ymin,ymax = ymax,fill = NA))

我希望这个功能可以让我在较不复杂的情况下构建更复杂的情节。我使用的函数返回一个网格对象,然后在完成组装后打印出最终的绘图。基线图中有一条单独的行,并在其周围有一个相应的错误栏( geom_ribbon )。更复杂的情节会有多行,多个重叠的 geom_ribbon 对象会让人分心。我想用多行删除它们。此外,我将能够使用方面或其他ggplot2功能快速创建替代版本。






编辑: 在工作时接受@ mnel的答案。现在,我需要确定如何动态访问<!c $ c> geom_ribbon 图层,该图层在SO问题中捕获



现在让我们删除标签并再次显示图:

 p < -  remove_geom(p,GeomText)
p


I'd like to remove a layer (in this case the results of geom_ribbon) from a ggplot2 created grid object. Is there a way I can remove it once it's already part of the object?

library(ggplot2)
dat <- data.frame(x=1:3, y=1:3, ymin=0:2, ymax=2:4)
p <- ggplot(dat, aes(x=x, y=y)) + geom_ribbon(aes(ymin=ymin, ymax=ymax), alpha=0.3) 
     + geom_line()

# This has the geom_ribbon
p

# This overlays another ribbon on top
p + geom_ribbon(aes(ymin=ymin, ymax=ymax, fill=NA))

I'd like this functionality to allow me to build more complicated plots on top of less complicated ones. I am using functions that return a grid object and then printing out the final plot once it is fully assembled. The base plot has a single line with a corresponding error bar (geom_ribbon) surrounding it. The more complicated plot will have several lines and the multiple overlapping geom_ribbon objects are distracting. I'd like to remove them from the plots with multiple lines. Additionally, I'll be able to quickly create alternative versions using facets or other ggplot2 functionality.


Edit: Accepting @mnel's answer as it works. Now I need to determine how to dynamically access the geom_ribbon layer, which is captured in the SO question here.


Edit 2: For completeness, this is the function I created to solve this problem:

remove_geom <- function(ggplot2_object, geom_type) {
  layers <- lapply(ggplot2_object$layers, function(x) if(x$geom$objname == geom_type) NULL else x)
  layers <- layers[!sapply(layers, is.null)]

  ggplot2_object$layers <- layers
  ggplot2_object
}

解决方案

For ggplot2 version 2.2.1, I had to modify the proposed remove_geom function like this:

remove_geom <- function(ggplot2_object, geom_type) {
  # Delete layers that match the requested type.
  layers <- lapply(ggplot2_object$layers, function(x) {
    if (class(x$geom)[1] == geom_type) {
      NULL
    } else {
      x
    }
  })
  # Delete the unwanted layers.
  layers <- layers[!sapply(layers, is.null)]
  ggplot2_object$layers <- layers
  ggplot2_object
}

Here's an example of how to use it:

library(ggplot2)

set.seed(3000)
d <- data.frame(
  x = runif(10),
  y = runif(10),
  label = sprintf("label%s", 1:10)
)

p <- ggplot(d, aes(x, y, label = label)) + geom_point() + geom_text()

Let's show the original plot:

p

Now let's remove the labels and show the plot again:

p <- remove_geom(p, "GeomText")
p

这篇关于从ggplot2图表中移除图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆