[R] ggplot2: Remove geom

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jul 15 19:40:20 CEST 2013


On Mon, Jul 15, 2013 at 1:08 PM, Thaler,Thorn,LAUSANNE,Applied
Mathematics <Thorn.Thaler at rdls.nestle.com> wrote:
> Dear all,
>
> Is there a possibility to remove a geom from a ggplot? Background suppose I have a function which returns a ggplot object after some data re-formatting and aggregation. While this ggplot object is fine in 90% of the cases it turns out that for some cases I want to suppress one of the layers which was added to the plot.
>
> I could look at the source code of the function and write a new one, which has an additional flag parameter, with which I could ask the function to add or not to add the geom, but this sounds a bit of overkill to me and it would be nice, if I could just remove the particular layer?
>
> An example is in order to make my point clearer:
>
> library(ggplot2)
> d <- data.frame(x=rep(1:10, each = 10), y = rnorm(100), grp = rep(1:10, 10))
>
> makePlot <- function() {
>   ggplot(d, aes(x = x, y = y)) + stat_summary(fun.data = "mean_cl_normal", color = "red") + geom_point()
> }
>
> (p <- makePlot())
>
> ## Now I want to have lines instead of points, but of course the points are still there
> p + geom_line(aes(group = grp, color = grp))
>
> Again, it would not be difficult to rewrite makePlot to deal with that, but for me this seems to be error prone / duplication. So ideally, I would like to do something like:
>
> p %-% geom_point()
>
> which is ambiguous of course, as there can be several point layers in the plot. Looking at
>
> str(p)
>
> I see that there is a layers slot, so I can do
>
> q <- p
> q$layers <- q$layers[-2]
> q + geom_line(aes(group = grp, color = grp))
>
> which does actually what I want.
>
> However, is there a way to do that in a more automated way? For now I have to inspect the object and to decide which layer I want to delete, otherwise I can get something like this:
>
> makePlot2 <- function() {
>   ggplot(d, aes(x = x, y = y)) + geom_point() + stat_summary(fun.data = "mean_cl_normal", color = "red")
> }
> q <- makePlot2()
> q$layers <- q$layers[-2]
> q + geom_line(aes(group = grp, color = grp))
>
> which removes the error bars but not the points (as the order in layers changed). So any ideas how to proceed in this case? If there is a nice way, I could even think of overloading %-% which would fit nicely in the idea of a plot which can not only be added layer by layer, but where I also could remove certain layers.
>
> Maybe (and even probable) there is a very good idea why I should not do that at all and I would be curious to hear these things as well. For now I am yet interested to know how I can remove layers of a plot conveniently.
>

This seems to show which layer is which:

> sapply(p$layers, function(x) x$geom$objname)
[1] "pointrange" "point"


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list