Skip to content

data.table not compatible with ggplot when it was generated from empty data.table #4597

Closed
@matthewgson

Description

@matthewgson

Hi,

I think I found a bug case where data.table does not work with ggplot.
Here's an example:

dt = data.table()
dt[, x := seq(1,100)]
dt[, y := seq(100,1)]
head(dt)
   x   y
1: 1 100
2: 2  99
3: 3  98
4: 4  97
5: 5  96
6: 6  95

# generate ggplot

ggplot(dt, aes(x,y)) + geom_point()

Error in `$<-.data.frame`(x, name, value) : 
  replacement has 1 row, data has 0

A workaround was

dt %>% setDF %>% setDT # convert to DF and convert it back

ggplot(dt, aes(x,y)) + geom_point() # now it works

which doesn't seem convenient.

I'm not certain if it is coming from data.table or ggplot, for plot(dt) just works fine, but I guessed it was from data.table side because setDT somehow resolves the issue. Or was I naive to use empty data.table from scratch?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions