Is there a simpler way in ggplot
to plot columns by their indices, as in function below?
plotYofX <- function(dt,x,y) {
dt[, lapply(.SD, function(x) {as.numeric(x)}), .SDcols = c(x,y)]
ggplot(dt) + geom_step(aes(x=get(names(dt)[x]), y=get(names(dt)[y]))) + labs(x=names(dt)[x], y=names(dt)[y])
Typing every time get(names(dt)[x]
and then also correcting axis names using labs
(e.g. instead of just writing aes(x,y)
) seems quite convoluted for such a smart package as ggplot2
...
This ggplot example is taken from discussion here: Don't want original data.table to be modified when passed to a function.