Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subset parameter in layers is no longer working with ggplot2 >= 2.0.0

Tags:

r

ggplot2

plyr

I updated to the newest version of ggplot2 and run into problems by printing subsets in a layer.

library(ggplot2)
library(plyr)
df <- data.frame(x=runif(100), y=runif(100))
ggplot(df, aes(x,y)) + geom_point(subset=.(x >= .5))

These lines of code worked in version 1.0.1 but not in 2.0.0. It throws an error Error: Unknown parameters: subset.

I couldn't find an official change log or a way how to subset specific layers. Specially because this plyr solution was not very nice documented, I think I found it somewhere in stack overflow.

like image 493
drmariod Avatar asked Jan 04 '16 09:01

drmariod


1 Answers

According to the comments in the ggplot2 2.0.0 code:

#' @param subset DEPRECATED. An older way of subsetting the dataset used in a
#'   layer.

Which can be found here: https://github.com/hadley/ggplot2/blob/34d0bd5d26a8929382d09606b4eda7a36ee20e5e/R/layer.r

One way to do that now would be this:

library(ggplot2)
library(plyr)
df <- data.frame(x=runif(100), y=runif(100))
ggplot(df, aes(x,y)) + geom_point(data=df[df$x>=.5,])

or this, (but beware of "Non Standard Evaluation" (NSE) :)

library(ggplot2)
library(plyr)
df <- data.frame(x=runif(100), y=runif(100))
ggplot(df, aes(x,y)) + geom_point(data=subset(df,x>=.5))

I think this is the one considered most safe as it has no NSE or dollar-sign field selectors:

library(ggplot2)
library(plyr)
df <- data.frame(x=runif(100), y=runif(100))
ggplot(df, aes(x,y)) + geom_point(data=df[df[["x"]]>=.5,])

But there are lots of others using pipes, etc...

like image 68
Mike Wise Avatar answered Oct 20 '22 20:10

Mike Wise