简体   繁体   English

按因子添加颜色时R图未显示数据点

[英]R plot not showing data points when adding colour by factor

I am trying to plot an R graph with data points coloured by a factor. 我正在尝试绘制一个R图,其中数据点由一个因子着色。 I am using the property col with values from a data frame column as the factor, but they seem not to be recognised correctly. 我正在使用属性col和数据帧列中的值作为因子,但是似乎无法正确识别它们。

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. 在绘制此图时,仅显示cor_partner值为1的数据点。 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. 如果我指定颜色(我认为0可能会读为NULL并返回白色),则显示所有数据点,但使用第一种指定颜色。

I have tried converting cor_partner to characters, but nothing changed. 我尝试将cor_partner转换为字符,但没有任何变化。

Anyone knows what is happening? 有人知道发生了什么吗? Thanks 谢谢

The parameter col = 0 in plot() indicates no color, you can see this via colors() . plot()的参数col = 0表示没有颜色,您可以通过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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM