0

I am trying to plot an R graph with data points coloured by a factor. I am using the property col with values from a data frame column as the factor, but they seem not to be recognised correctly.

My code is as follows:

plot(marrmales$tot_nochc, xlab="", ylab="Tot hours worked",   col=marmales.df$cor_partner, pch=15)

marmales.df$cor_partner is a vector of 0 and 1.

When I plot this, only data points for which the cor_partner value is 1 are shown. If I specify colours (I thought 0 might be read as NULL and return white), all data points are shown, but in the first of the specified colours.

I have tried converting cor_partner to characters, but nothing changed.

Anyone knows what is happening? Thanks

InverniE
  • 598
  • 1
  • 7
  • 21
  • 1
    is the following link any use, http://stackoverflow.com/questions/7721262/colouring-plot-by-factor-in-r ? – DarrenRhodes Jun 27 '16 at 14:02
  • @user1945827 No, that is the same approach that I am using. The problem is that the factor I specify in col is not recognised correctly. – InverniE Jun 27 '16 at 14:30
  • It may help you to read up on the meaning of `factor`. $cor_partner appears not to be a factor. It looks like it is an integer or a numeric (you did not include your data, so it is not possible to be certain which). – dww Jun 27 '16 at 22:24

1 Answers1

1

The parameter col = 0 in plot() indicates no color, you can see this via colors().

A solution to your problem can be:

plot(marrmales$tot_nochc, xlab="", 
     ylab="Tot hours worked", 
     col=as.factor(marmales.df$cor_partner), 
     pch=15)
J_F
  • 9,956
  • 2
  • 31
  • 55
  • It works, thanks! Do you also know why I can't get the same result with as.character(marmales.df$cor_partner)? – InverniE Jun 27 '16 at 14:26