I\'m trying to make use of advanced tricks from data.table and ggplot2 functionalities to create a simple yet powerful function that automatically
I hope this works for you:
plotAllXYbyZ <- function(dt, x, y, z) {
# to make sure all columns to be melted for ploting are numerical
dt[, (y):= lapply(.SD, function(x) {as.numeric(as.character(x))}), .SDcols = y]
dts <- melt(dt, id = c(x,z), measure = y)
ggplot(dts, aes_string(x = colnames(dt)[x], y = "value", colours = colnames(dt)[z])) +
geom_line() + facet_wrap(~ variable)
}
dt <- data.table(mtcars)
plotAllXYbyZ(dt, x=1, y=3:10, z=2)