0

I am trying to plot a couple of feature columns against the categories for a data.table. However, I am not get color coding by category which happens automatically when a similar formatted iris dataset is plotted.

library(data.table)
library(caret)

dtData <- data.table(
  Index = seq(1:9),
  ColumnA = as.numeric(c(1018,1051,979,1036,963,1055,1063,968,1036)),
  ColumnB = as.numeric(c(566,600,529,530,516,586,562,514,537)),
  Category = as.factor(c("hybrid","mountain","road","hybrid","road","mountain","mountain","road","hybrid"))
  )

featurePlot(x=dtData[,2:3],y = dtData[,4], plot="pairs")

When I run the code above, I get the following plot:

enter image description here

It is not colored by category and instead the category also appears as a column which is not as expected. Any pointers on resolving this?

Thanks much.

markus
  • 25,843
  • 5
  • 39
  • 58
noddy157
  • 31
  • 4
  • Can you try with `y = dtData[[4]]` – markus Jul 30 '20 at 12:03
  • whew.. that seems to be doing the job. But I do not understand why that works and not dtData[,4]. Would still keep the question open for a more elegant solution to the question? Thanks Markus! :) – noddy157 Jul 30 '20 at 12:09
  • 1
    Glad it worked. I would not expect a more elegant solution here. The difference is that `dtData[,4]` returns a data.table while `dtData[[4]]` returns a factor, which is what the function expects. Possible duplicate: https://stackoverflow.com/questions/20043313/extract-a-column-from-a-data-table-as-a-vector-by-position – markus Jul 30 '20 at 12:11
  • Thanks Markus. One additional question then: is there a better function than featurePlot for plotting datatable feature columns? – noddy157 Jul 30 '20 at 12:30

0 Answers0