I have two very similar data frames, which ggplot2
sees differently; although the contents are the same the data structures are subtly different. One is a data.frame
, the other a data_frame
. I'd like to understand the difference in how ggplot2
sees them. In the following examples, both are being used in a stat_function
; the data.frame
produces plots while the data_frame
produces errors. This is particularly confusing in light of the interoperability of packages in the Hadleyverse. I first ran into this issue when I found that I was unable to create a plot from a data frame produced by dplyr (dplyr turns data.frames into data_frames) while a data frame I thought was identical (it wasn't, it was a data.frame) worked just fine.
Example 1
First, the working version from the data.frame
.
library(ggplot2)
library(dplyr)
d.f <- data.frame(mean = 0, sd = 1)
d_f <- data_frame(mean = 0, sd = 1)
ggplot(data.frame(x=-3:3), aes(x)) +
stat_function(fun = function (x) dnorm(x, mean = d.f[1,1], sd = d.f[1,2]))
And now the non-working version from the data_frame
.
ggplot(data.frame(x=-3:3), aes(x)) +
stat_function(fun = function (x) dnorm(x, mean = d_f[1,1], sd = d_f[1,2]))
## Warning message:
## Computation failed in `stat_function()`:
## Non-numeric argument to mathematical function
Example 2
This example produces a different error message though perhaps the underlying issue is the same. First, the working version with a data.frame
.
logistic <- function (x) { 1/(1 + exp(-x)) }
d.f <- data.frame(b0 = -9, b1 = 0.8)
d_f <- data_frame(b0 = -9, b1 = 0.8)
ggplot(data.frame(x=0:20), aes(x)) +
stat_function(fun = function (x) logistic(d.f[1,1] + d.f[1,2] * x))
And here's the non-working version with a data_frame
.
ggplot(data.frame(x=0:20), aes(x)) +
stat_function(fun = function (x) logistic(d_f[1,1] + d_f[1,2] * x))
## Error in eval(expr, envir, enclos) : object 'y' not found