Tim
Tim

Reputation: 99428

How to plot points using their class labels?

I have a 100 2-dim points, which form a 100 by 2 matrix X, stored in a text file "data"

I have a 100-dim vector Y, which form the class labels (numerical from 1 to 3) of the 100 points, and is stored in a text file "labels".

In R, I was wondering how you would plot the 2-dim points in X, s.t. each point is represented by its class label instead of a dot and represented in a color of its class label (the color is same for points of the same class label, but different for points of different class labels)?

Thanks!

Upvotes: 0

Views: 4709

Answers (2)

joran
joran

Reputation: 173577

To make Dirk happy, a non-ggplot answer:

x1 <- runif(100)
x2 <- runif(100)
y  <- sample.int(3 , 100 , replace = T)

plot(x1,x2,type = "n")
text(x1,x2,labels = y,col = c('red','blue','green')[y])

enter image description here

Upvotes: 2

Simon O&#39;Hanlon
Simon O&#39;Hanlon

Reputation: 59970

Do you want to do something like this?

x1 <- runif(100)
x2 <- runif(100)
y  <- sample.int(3 , 100 , replace = T)

df <- data.frame( x1,x2,y)
ggplot( df )+
geom_text( aes( x1 , x2 , label = y , colour = factor(y)))

plot

Upvotes: 3

Related Questions