在 R 中按类别着色点 [英] Coloring the points by category in R
问题描述
我正在使用以下代码在 R 中创建散点图:
I am creating a scatter plot in R using the following code:
plot(df_prob1$x1, df_prob1$x2, pch = df_prob1$y)
我得到以下情节:
如上图所示,有两类,一类用正方形表示,另一类用圆形表示.我希望这两个类别也有不同的颜色.
As seen in the above plot there are two categories, one represented by a square and the other by circle. I want these two categories to have different colors as well.
我确实尝试使用以下代码:
I did try using the following code:
plot(df_prob1$x1, df_prob1$x2, pch = df_prob1$y, col = c("red", "blue"))
我得到以下情节:
然而,它是随机着色点,没有考虑类别.
However, it is randomly coloring points and not taking the categories into consideration.
我也尝试将变量作为 col 的值传递:
I also did trying passing the variable as value for col as such:
plot(df_prob1$x1, df_prob1$x2, pch = df_prob1$y, col = df_prob1$y)
但这并没有给出正确的情节.
But this didn't give a proper plot.
推荐答案
诀窍是使用 df_prob1$y
作为颜色向量的索引,c("red", "蓝色")
.如果将列 y
强制转换为因子,则可以轻松完成此操作,因为因子在内部编码为从 1 开始的连续整数.以下代码使用内置数据集 iris代码>,在此答案的末尾处理.
The trick is to use df_prob1$y
as an index to the colors vector, c("red", "blue")
. This can easily be done if the column y
is coerced to a factor, since factors are coded internally as consecutive integers starting at 1. The following code uses the built-in data set iris
, processed at the end of this answer.
clrs <- c("red", "blue")[factor(df_prob1$y)]
plot(df_prob1$x1, df_prob1$x2, pch = df_prob1$y, col = clrs)
测试数据.
set.seed(1234)
df_prob1 <- subset(iris[c(1, 2, 5)], Species != "virginica")
df_prob1 <- df_prob1[sample(nrow(df_prob1), 50), ]
df_prob1[[3]] <- as.numeric(df_prob1[[3]] == "setosa")
names(df_prob1) <- c("x1", "x2", "y")
这篇关于在 R 中按类别着色点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!