1
votes

I am using plot function on mtcars dataset.

I am trying to add colour to the plots based on mtcars$cyl variable

The distinct values in cyl variable is 4,6 and 8

First i tried this:

plot(x=mtcars$wt, y=mtcars$mpg, col = mtcars$cyl)

I got points plotted on blue,purple and grey colour.

Then I converted cyl variable into a factor and tried the same plot again,

mtcars$fcyl <- as.factor(mtcars$cyl)

plot(x=mtcars$wt, y=mtcars$mpg, col = mtcars$fcyl)

But this time I got black,red and green

I want to understand how assigning the variable as factor changes the colour. What happens behind?

1
factor is converting your continuous data in categorial data (basically integers), the factors may not be in the same order as the sorted numbers, and I guess with factors plot try to space evenly the values on the palette. With continuous values obviously two close value will be assigned to close colors. - Moody_Mudskipper

1 Answers

2
votes

I want to understand how assigning the variable as factor changes the colour. What happens behind?

In R, factors are just integers under the hood. In the plot function, integers are converted to eight different colors (repeating), which are 8 colors that can be visually separated.

Try this:

plot(x=1:16, y=1:16, col = 1:16, pch=16)