Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do two ggplot objects pass an all.equal() test, but fail identical() test?

Tags:

I want to test if two graphs generated by ggplot are the same. One option would be to use all.equal on the plot objects, but I'd rather have a harder test to ensure they're the same, which seems like is something identical() provides me.

However, when I tested two plot objects created with the same data and the same aes, I've found that all.equal() recognizes them as being the same, whereas the objects didn't pass the identical test. I'm not sure why and I'd love to learn more.

Basic example:

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 
like image 585
Kevin Carriere Avatar asked Jan 01 '18 20:01

Kevin Carriere


People also ask

What is the first argument in the ggplot () function?

The first argument is the source of the data. The second argument maps the data components of interest into components of the graph. That argument is a function called <code>aes()</code>, which stands for <em>aes</em>thetic mapping.

How does ggplot function work?

ggplot2 is a plotting package that provides helpful commands to create complex plots from data in a data frame. It provides a more programmatic interface for specifying what variables to plot, how they are displayed, and general visual properties.

How do I add two ggplot together?

Combine multiple ggplot on one page.Use the function ggarrange() [ggpubr package], a wrapper around the function plot_grid() [cowplot package]. Compared to plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.

What is the difference between ggplot and plot?

Base R plots two vectors as x and y axes and allows modifications to that representation of data whereas ggplot2 derives graphics directly from the dataset. This allows faster fine-tuning of visualizations of data rather than representations of data stitched together in the Base R package1.


1 Answers

The graph and graph2 objects contain environments and each time an environment is generated it is different even if it holds the same values. R lists are identical if they have the same contents. This can be stated by saying that environments have object identity apart from their values whereas the values of the list form the identity of the list. Try:

dput(graph) 

giving the following which includes environments denoted by <environment> in the dput output: (continued after output)

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

For example, consider:

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 
like image 90
G. Grothendieck Avatar answered Sep 19 '22 15:09

G. Grothendieck