繁体   English   中英

plot 具有不同年份的不同颜色 - R

[英]plot having different color for different year - R

如何使用以下矩阵为每年使用相同的颜色(例如,我为 1011 拥有的三行,我希望全部为红色)。 我尝试了plot(All[,1:2],col=1:4) ,它为 plot 中的每个点提供不同的颜色。 知道如何解决吗?

欣赏!

structure(c(1564.7, 1310.72727272727, 1063.46153846154, 1164.77777777778, 
1284.52941176471, 868.214285714286, 2610.83333333333, 929.47619047619, 
1121.2, 3130.6, 2110.77272727273, 3600.54545454545, 2096.96296296296, 
1688.91666666667, 1371.03846153846, 12610.4, 14047.6363636364, 
11548.2857142857, 14474.3333333333, 16720.9411764706, 15759.5714285714, 
18197.4166666667, 9571.80952380952, 6553, 11778.4, 11159.2727272727, 
12094.2727272727, 11003, 8450.25, 9756.46153846154, 15.6, 17.5454545454545, 
13.7142857142857, 15.1111111111111, 17.4705882352941, 16.3571428571429, 
19.4166666666667, 13.0952380952381, 10.4, 17.8, 16.6363636363636, 
18, 15.8888888888889, 11.25, 13.1538461538462, 1011, 1011, 1011, 
1112, 1112, 1112, 1112, 1213, 1213, 1213, 1213, 1314, 1314, 1314, 
1314), .Dim = c(15L, 4L), .Dimnames = list(NULL, c("NumAct", 
"TR", "Gr", "Year")))

可以使用All[,4]访问Year值,并将其作为col参数传递将根据该列的值为您提供颜色。

plot(All[,1:2],col=All[,4]) 

output

检验出

更新

要添加图例,您可以执行以下操作(如果需要,我可以解释):

legend("topleft", legend=names(table(ALL[,4])), pch=1, col=unique(names(table(ALL[,4]))))

Output - 2

输出更新

要添加图例,您可以使用以下代码,尽管它是手动代码,但工作正常

绘图

plot(All[,1:2],col=c("coral4","springgreen4","magenta","black"),pch=19) 

添加图例

legend(locator(1),legend=c("1011","1112","1213","1314"),col=c("coral4","springgreen4","magenta","black"),pch=19,bty="n",cex=c(0.8,0.8,0.8,0.8))

[1]:https://i.stack.imgur.com/5VrBk.png

暂无
暂无

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

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