jf328
jf328

Reputation: 7351

ggplot on datatable string column error

Could someone tell me why the second ggplot fails? How can I change a string column in data table and use ggplot?

dt = data.table(name = c("aaa", "bbb", "ccc"), value = 1:3)
ggplot(dt) + geom_point(aes(x = name, y=value)) # this works
dt[, name := lapply(name, function (x) substring(x, 2) )]
dt
   name value
1:   aa     1
2:   bb     2
3:   cc     3
ggplot(dt) + geom_point(aes(x = name, y=value)) # now fails

Don't know how to automatically pick scale for object of type list. Defaulting to continuous
Error: geom_point requires the following missing aesthetics: x

Upvotes: 1

Views: 67

Answers (1)

akrun
akrun

Reputation: 887048

We don't need lapply here

dt[, name:= substring(name, 2)]

Now, the ggplot code should work.

By using lapply, we are creating a list with 3 elements instead of a column with 3 elements

Upvotes: 3

Related Questions