Coloring the points by category in R

青春壹個敷衍的年華 提交于 2021-02-11 14:05:35

问题


I am creating a scatter plot in R using the following code:

plot(df_prob1$x1, df_prob1$x2, pch = df_prob1$y)

I get the following plot:

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"))

And I get the following plot:

However, it is randomly coloring points and not taking the categories into consideration.

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.


回答1:


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)

Test data.

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")



回答2:


You can use ggplot library for this :

library(ggplot) #install it if you dont have

ggplot(df_prob1,aes(x1,x2))+geom_point(aes(color = factor(y), shape = factor(y))) 

r statistics plot



来源:https://stackoverflow.com/questions/58987822/coloring-the-points-by-category-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!