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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With