ggplot によって生成された 2 つのグラフが同じかどうかをテストしたいです。 1 つのオプションはall.equal
、プロット オブジェクトを使用することですが、同じであることを確認するために、より厳しいテストを行いたいのですが、それがidentical()
私に提供しているもののように思えます。
しかし、私が作成した2つのプロットオブジェクトをテストしたところ、同じ 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
ベストアンサー1
およびオブジェクトにgraph
はgraph2
環境が含まれており、環境が生成されるたびに、同じ値を保持している場合でも環境は異なります。R リストは、内容が同じであれば同一です。これは、環境は値とは別にオブジェクト ID を持ち、リストの値はリストの ID を形成すると述べることで説明できます。次を試してください。
dput(graph)
<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"))
たとえば、次のことを考慮してください。
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) # the values are the same
## [1] TRUE
identical(g, g2) # but they are not identical
## [1] FALSE