Ángel
Ángel

Reputation: 145

Plot factor levels in R

I'm using the iris data, previously changed the species factor from "setosa" "versicolor" and "virginica" to "s", "c" and "v". if I code this: plot(iris$Sepal.Length, iris$Petal.Length, col = iris$Species) The data are point which can change for example, to a square: plot(iris$Sepal.Length, iris$Petal.Length, col = iris$Species, pch = 15) but, how I could show the species factor like this graph?

graph

Upvotes: 1

Views: 3661

Answers (1)

catastrophic-failure
catastrophic-failure

Reputation: 3905

Here we use the factor as an index to labels:

plot(iris$Sepal.Length, iris$Petal.Length, col = as.numeric(iris$Species)+1,
  pch = c("s", "c", "v")[as.numeric(iris$Species)])

Resulting plot:

2D iris data subset plotted with letters as symbols

Upvotes: 3

Related Questions