data.table and Construct aesthetic mappings aes(y = ..density..)

Why do I receive the following alert when using:
aes (y = ..density ..)

Error in `[.data.table`(df, , ggplot(.SD, aes(x = max.diff)) + geom_histogram(aes(y = ..density..),  : 
  Variable 'density..' is not found in calling scope. Looking in calling scope because this symbol was prefixed with .. in the j= parameter.

But using this one, it works:
foo <- "..density.."
aes_string(y = foo)

df<-structure(list(max.diff = c(6.02, 7.56, 7.79, 7.43, 7.21, 7.65, 
                            8.1, 7.35, 7.57, 9.09, 6.21, 8.2, 6.82, 7.18, 7.78, 8.27, 6.85, 
                            6.72, 6.67, 6.99, 7.32, 6.59, 6.86, 6.02, 8.5, 7.25, 5.18, 8.85, 
                            5.44, 6.44, 7.85, 6.25, 9.06, 8.19, 5.08, 6.26, 8.92, 6.83, 6.5, 
                            7.55, 7.31, 5.83, 5.55, 4.29, 8.29, 8.72, 9.5)), 
          class = "data.table", row.names = c(NA, -47L), .Names = "max.diff")
df=setDT(df)

foo <- "..density.."
df[,ggplot(, aes(x = max.diff)) +
  geom_histogram(aes_string(y = foo), colour="white") +
  ggtitle("geom_histogram; default parameters"),]

df[,ggplot(.SD, aes(x = max.diff)) +
  geom_histogram(aes(y = ..density..), colour="white") +
  ggtitle("geom_histogram; default parameters"),]

The problem may be that the prefix of .. is already used by data.table for non-standard evaluation.

If you don't process the ggplot() call within the df[] call then it will work:

ggplot(df, aes(x = max.diff)) +
     geom_histogram(aes(y = ..density..), colour="white") +
     ggtitle("geom_histogram; default parameters")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.