Kev*_*ere 22 equality r ggplot2
我想测试ggplot生成的两个图是否相同.一种选择是all.equal
在情节对象上使用,但我宁愿进行更难的测试以确保它们是相同的,这似乎是identical()
我提供的东西.
但是,当我测试两个使用相同 data
和相同 创建的绘图对象时aes
,我发现它们all.equal()
识别它们是相同的,而对象没有通过identical
测试.我不确定为什么,我想要了解更多.
基本示例:
graph <- ggplot2::ggplot(data = iris, aes(x = Species, y = Sepal.Length))
graph2 <- ggplot2::ggplot(data = iris, aes(x = Species, y = Sepal.Length))
all.equal(graph, graph2)
# [1] TRUE
identical(graph, graph2)
# [1] FALSE
Run Code Online (Sandbox Code Playgroud)
G. *_*eck 23
的graph
和graph2
对象包含环境和环境产生它每次是不同的,即使它保持相同的值.如果R列表具有相同的内容但是列表环境具有对象标识,则R列表是相同的.尝试:
dput(graph)
Run Code Online (Sandbox Code Playgroud)
给予其包括由表示环境以下<environment>
的dput
输出:(输出后续)
...snip...
), class = "factor")), .Names = c("Sepal.Length", "Sepal.Width",
"Petal.Length", "Petal.Width", "Species"), row.names = c(NA,
-150L), class = "data.frame"), layers = list(), scales = <environment>,
mapping = structure(list(x = Species, y = Sepal.Length), .Names = c("x",
"y"), class = "uneval"), theme = list(), coordinates = <environment>,
facet = <environment>, plot_env = <environment>, labels = structure(list(
x = "Species", y = "Sepal.Length"), .Names = c("x", "y"
))), .Names = c("data", "layers", "scales", "mapping", "theme",
"coordinates", "facet", "plot_env", "labels"), class = c("gg",
"ggplot"))
Run Code Online (Sandbox Code Playgroud)
例如,考虑:
g <- new.env()
g$a <- 1
g2 <- new.env()
g2$a <- 1
identical(as.list(g), as.list(g2))
## [1] TRUE
all.equal(g, g2)
## [1] TRUE
identical(g, g2)
## [1] FALSE
Run Code Online (Sandbox Code Playgroud)