如何在R中的散点图中为每个类别指定颜色?
在数据集中,我想采用两个属性并创建监督散点图。有谁知道如何为每个班级赋予不同的颜色?
我正在尝试在绘图命令中使用 col == c("red","blue","yellow")
但不确定它是否正确,就好像我包含了另一种颜色,即该颜色即使我只有 3 个类,也出现在散点图中。
谢谢
In a dataset, I want to take two attributes and create supervised scatter plot. Does anyone know how to give different color to each class ?
I am trying to use col == c("red","blue","yellow")
in the plot command but not sure if it is right as if I include one more color, that color also comes in the scatter plot even though I have only 3 classes.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这篇文章很旧,但我花了很长时间试图弄清楚这一点,所以我想我会发布更新的回复。我的主要来源是这个精彩的 PowerPoint:http://www.lrdc。 pitt.edu/maplelab/slides/14-Plotting.pdf。好的,这就是我所做的:
在这个示例中,我的数据集称为“数据”,我将“触摸”数据与“凝视”数据进行比较。受试者被分为两组:“红色”和“蓝色”。
这组代码创建了我的 Blue 组的 Touch v Gaze 散点图
par(new = TRUE)
这告诉 R 创建一个新图。当您一起运行所有代码时,第二个图会自动覆盖第一个图
plot(Data$Touch[Data$Category == "Red"], Data$Gaze[Data$Category == "Red"],axes = FALSE, xlab = "", ylab = "", col =“红色”,pch = 2)
这是第二个图。我在编写这些代码时发现,R 不仅将数据点放置在蓝色图上,而且还放置了坐标区、坐标区标题和主标题。
为了摆脱烦人的重叠问题,我使用axes函数来摆脱轴本身并将标题设置为空白。
图例(x = 60, y = 50, legend = c("蓝色", "红色"), col = c("蓝色", "红色"), pch = c(20, 2) )
添加一个漂亮的图例来完善项目
这种方式可能比漂亮的 ggplots 长一点,但我今天不想学习全新的东西,希望这对某人有帮助!
This article is old, but I spent a hot minute trying to figure this out so I figured I would post an updated response. My main source is this wonderful PowerPoint: http://www.lrdc.pitt.edu/maplelab/slides/14-Plotting.pdf. Okay, here's what I did:
In this example, my data set is called 'Data' and I was comparing 'Touch' data against 'Gaze' data. The subjects were divided into two groups: 'Red' and 'Blue'.
This set of code creates a scatterplot of Touch v Gaze of my Blue group
par(new = TRUE)
This tells R to create a new plot. This second plot is laid over the first automatically by R when you run all the code together
plot(Data$Touch[Data$Category == "Red"], Data$Gaze[Data$Category == "Red"], axes = FALSE, xlab = "", ylab = "", col = "red", pch = 2)
This is the second plot. I found when I was coding these that R didn't just lay over the data points onto the Blue plot, but it also lay the axes, axes titles, and main title.
To get rid of the annoying overlap problem, I used the axes function to get rid of the axes themselves and set the titles to be blank.
legend(x = 60, y = 50, legend = c("Blue", "Red"), col = c("blue", "red"), pch = c(20, 2))
Adding a pretty legend to round out the project
This way may be a bit longer than the pretty ggplots but I did not want to learn something completely new today, hope this helps someone!
这是使用传统图形(和 Dirk 数据)的解决方案:
这依赖于
DF$z
是一个因子的事实,因此当用它进行子集化时,其值将被视为整数。因此,颜色向量的元素将随z
变化,如下所示:您可以使用
legend
函数添加图例:Here is a solution using traditional graphics (and Dirk's data):
This relies on the fact that
DF$z
is a factor, so when subsetting by it, its values will be treated as integers. So the elements of the color vector will vary withz
as follows:You can add a legend using the
legend
function:以下是我基于此页面构建的示例。
这给你输出。从这张图中你可以很容易地发现错误分类的物种。
Here is an example that I built based on this page.
This gives you the output. You can easily spot the misclassified species from this figure.
一种方法是使用lattice包和xyplot():
通过变量
z
给出显式分组信息,您可以获得不同的颜色。您可以指定颜色等,请参阅点阵文档。因为这里的 z 是一个因子变量,我们可以为其获取级别(==数字索引),所以您也可以这样做
,但这不太透明(至少对我来说是这样:)然后 xyplot( )等。
One way is to use the lattice package and xyplot():
By giving explicit grouping information via variable
z
, you obtain different colors. You can specify colors etc, see the lattice documentation.Because
z
here is a factor variable for which we obtain the levels (== numeric indices), you can also dobut that is less transparent (to me, at least :) then
xyplot()
et al.以下是我在 2018 年的做法。谁知道呢,也许 R 新手有一天会看到它并爱上 ggplot2。
Here is how I do it in 2018. Who knows, maybe an R newbie will see it one day and fall in love with
ggplot2
.如果您将类分隔在数据框或矩阵中,则可以使用 matplot。例如,如果我们有
那么您将得到一个散点图,其中
dat
的第一列绘制为红色,第二列绘制为蓝色,第三列绘制为黄色。当然,如果您希望颜色类别有单独的 x 和 y 值,那么您可以使用datx
和daty
等。另一种方法是附加一个额外的值列指定您想要的颜色(或保留额外的颜色向量,使用
for
循环和一些if
分支迭代填充它)。例如,这将为您提供相同的情节:If you have the classes separated in a data frame or a matrix, then you can use
matplot
. For example, if we haveThen you'll get a scatterplot where the first column of
dat
is plotted in red, the second in blue, and the third in yellow. Of course, if you want separate x and y values for your color classes, then you can havedatx
anddaty
, etc.An alternate approach would be to tack on an extra column specifying what color you want (or keeping an extra vector of colors, filling it iteratively with a
for
loop and someif
branches). For example, this will get you the same plot:假设类变量是 z,您可以使用:
但是,z 是因子变量很重要,因为 R 在内部将因子存储为整数。
这样,1 是“黑色”,2 是“红色”,3 是“绿色”,......
Assuming the class variable is z, you can use:
however, it's important that z is a factor variable, as R internally stores factors as integers.
This way, 1 is 'black', 2 is 'red', 3 is 'green, ....