I'm trying to make use of advanced tricks from data.table
and ggplot2
functionalities to create a simple yet powerful function that automatically plots (in one image!) all columns (y) of an arbitrary data.table
as a function of input column (x), optinally conditioned by column (k) - So that we can quickly visualize all data columns using a single line like this:
dt <- data.table(diamonds[1:100,])[order(carat),cut:=as.character(cut)]
plotAllXYbyZ(dt)
plotAllXYbyZ(dt, x="carat", k="color")
plotAllXYbyZ(dt, x=1, y=c(2,8:10), k=3)
CLARIFICATION: The challenge is that columns can be of either type (numeric, character or factor). We want a function that deals with this automatically. - i.e. it should be able to plot all requested columns using melt and ggplot, as I'm trying in Answer below.
UPDATE: My code is posted below in Answer. It's functional (i.e. displays desired plots). However it has an issue, namely: It modifies the original data.table. - To address this issue I asked a new question here: Don't want original data.table to be modified when passed to a function