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

a 夏天 提交于 2019-11-30 03:00:25

问题


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.


回答1:


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...



来源:https://stackoverflow.com/questions/34588232/subset-parameter-in-layers-is-no-longer-working-with-ggplot2-2-0-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!