问题描述
我更新到最新版本的 ggplot2
,并通过在图层中打印子集来解决问题。
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))
这些代码行在 1.0.1
版本中工作,但不在 2.0.0
中。它会抛出一个错误错误:未知参数:子集
。
These lines of code worked in version 1.0.1
but not in 2.0.0
. It throws an error Error: Unknown parameters: subset
.
我找不到正式的更改日志或如何子集特定图层。特别是因为这个 plyr
解决方案没有很好记录,所以我认为我发现它在堆栈溢出的某个地方。
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.
推荐答案
根据ggplot2 2.0.0代码中的注释:
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.
可以在这里找到:
现在做到这一点的一种方法是:
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,])
或者这个(但是要小心Non标准评估):
or this, (but beware of "Non Standard Evaluation" :)
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))
我认为这是最安全的,因为它没有NSE或美元。
I think this is the one considered most safe as it has no NSE or dollars:
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...
这篇关于图层中的子集参数不再适用于ggplot2> = 2.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!