Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“的用法;col";R中的参数绘图函数_R - Fatal编程技术网

“的用法;col";R中的参数绘图函数

“的用法;col";R中的参数绘图函数,r,R,我在R中看到了一个绘图函数的示例,其中“col”参数的值是一个变量,而不是一个颜色值(例如“蓝色”、“红色”等)。例如,在下面的示例中,col参数指向变量“Region”,图形显示X轴上的年份和Y轴上的销售额,点为Region(三维)着色。我检查了绘图函数和颜色参数的文档,但没有迹象表明col参数可以采用变量名而不是颜色值。我想了解我是否正在查看正确的文档 year = c(1990:1994) sales = c(1:5) region = c('East','East','West','

我在R中看到了一个绘图函数的示例,其中“col”参数的值是一个变量,而不是一个颜色值(例如“蓝色”、“红色”等)。例如,在下面的示例中,col参数指向变量“Region”,图形显示X轴上的年份和Y轴上的销售额,点为Region(三维)着色。我检查了绘图函数和颜色参数的文档,但没有迹象表明col参数可以采用变量名而不是颜色值。我想了解我是否正在查看正确的文档

year = c(1990:1994)

sales = c(1:5)

region = c('East','East','West','North','South')

SalesData = data.frame(sales,year,region)   

with(SalesData,plot(year,sales,col=region))

尽管在R中生成绘图最常用的函数是
plot
,但您可以在
?par
文档中找到最有用的信息。其中有一个
颜色规范
部分,介绍如何为绘图设置颜色。这里有一段摘录:

 Colors can be specified in several different ways. The simplest
 way is with a character string giving the color name (e.g.,
 ‘"red"’).  A list of the possible colors can be obtained with the
 function ‘colors’.  Alternatively, colors can be specified
 directly in terms of their RGB components with a string of the
 form ‘"#RRGGBB"’ where each of the pairs ‘RR’, ‘GG’, ‘BB’ consist
 of two hexadecimal digits giving a value in the range ‘00’ to
 ‘FF’.  Colors can also be specified by giving an index into a
 small table of colors, the ‘palette’: indices wrap round so with
 the default palette of size 8, ‘10’ is the same as ‘2’.  This
 provides compatibility with S.  Index ‘0’ corresponds to the
 background color.  Note that the palette (apart from ‘0’ which is
 per-device) is a per-session setting.
请注意,在本例中,您不是将
字符
传递给
col
参数,而是传递基本上是整数值的
因子。此外,可以指定要打印的点的颜色,因此可以声明每个点的颜色

希望这有帮助。

你必须把“with(SalesData,plot(year,sales,col=SalesData$region))”改为“with(SalesData,plot(year,sales,col=SalesData$region)”
“使用(SalesData,plot(year,sales,col=region))”

如果要制作各种图形,请查找

有一个用于选择颜色的工具:


请记住,当您以十六进制输入一系列颜色时,请确保使用大写:例如col=c(“1B9E77”、“D95F02”、“7570B3”、“666666”)

请参见
?par
颜色规范
一节@nicola您可以从文档中复制一段摘录并将其作为答案发布。@nicola颜色规范没有提供任何此类信息。请阅读我下面的评论。在发布此问题之前,我已经阅读了文档中的此文本。文档中的这段文字只告诉您如何使用颜色值,如红色或十六进制代码等。但它在何处提到可以传递您在注释文字中提到的系数?。像我这样的学习者如何从阅读文档中知道col可以从数据集中获取一个可以是字段/变量的因子值?我想你提到的是一般的R概念。第一:将因子存储为整数。请参见示例:。第二:R中的一切都是对象。当您指定
col=“red”
时,您正在创建一个长度为1且
red
为值的(匿名)
字符
向量,并将其作为
col
参数传递。您也可以在之前创建一个对象:
mycolb但是在我的示例中,字符向量包含区域值,例如“东”、“西”、“北”、“南”。它们不是“红色”、“蓝色”等颜色值!。你的意思是说“东”、“西”等值被转换为1和0吗。如果是这样,它们如何变成蓝色、红色等?同样,
区域
不是一个
字符
向量,而是一个
因子
。传递的是整数值。您的呼叫与
col=c(1,1,2,3,4)
相同。您是否阅读了我在前面的评论中提供的链接?
颜色规范
部分还解释了颜色索引是由默认的
调色板
获取的。尝试
palete()
获取默认值并读取
?palete