我在R中看到了一个绘图函数的例子,其中“col”参数的值是一个变量而不是一个颜色值(例如“蓝色”,“红色”等)。例如,在下面的示例中,col参数指向变量“Region”,图表显示X轴中的Year和Y轴上的Sales,其中Region为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))
答案 0 :(得分:2)
虽然用于在R中生成绘图的最常用函数是plot
,但您可以在?par
文档下找到最有用的信息。其中有一个Color Specification
部分,描述了如何为绘图设置颜色。这里有一段摘录:
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.
请注意,在您的情况下,您没有将character
传递给col
参数,而是factor
基本上是整数值。此外,您可以指定与要绘制的点数一样多的颜色,这样就可以声明每个点的颜色。
希望这有帮助。
答案 1 :(得分:0)
如果您要制作各种图表,请查看http://coleoguy.blogspot.de/2016/06/symbols-and-colors-in-r-pch-argument.html
有一种选择颜色的工具:http://colorbrewer2.org/
请记住,当您在HEX中输入一系列颜色时,请确保使用大写字母:例如COL = C( “#1B9E77”, “#D95F02”, “#7570B3”, “#666666”)
答案 2 :(得分:-1)
你必须把"与(SalesData,plot(year,sales,col = SalesData $ region))"代替 "与(SALESDATA,图(年,销售,COL =区域))"