1

Something I really like about R is that the plot() command lets you quickly see a lot of what is going on in a data frame

e.g.

library(datasets)
iris_df<-data.frame(iris)
plot(iris_df)

gives you this: enter image description here

Whilst this set of graphs is nice it doesn't deal well with categorical variables. Column 5 of our data is species, and whilst the last row of the graphs tells us how species relates to the other variables it would be nice to see how the clustering in the other graphs relates to species.

You can do this via

plot(iris_df[is.element(iris_df$Species, "versicolor"),])
plot(iris_df[is.element(iris_df$Species, "setosa"),])
plot(iris_df[is.element(iris_df$Species, "virginica"),])

but this gives you three separate plots. I'd like to combine them and visualise them with different colours so you can see how the clustering works in one image.

(I imagine this might be equivalent to using hold on in matlab )

1 Answer 1

3

You could use the col argument to change color and/or pch to change shape:

plot(iris, col = iris$Species, pch = 15 + as.numeric(iris$Species))

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

+1, however, 10% of men are red/green colorblind, & you have a lot of overlapping circles. This plot can be improved by using solid circles (pch=16), using black, red & blue, and using partial transparency (col = with(iris, ifelse(Species==1, rgb(0,0,0,alpha=.5), ifelse(Species==2, rgb(1,0,0,alpha=.5), rgb(0,0,1,alpha=.5))).
@gung , thanks for the comment. plot(iris, col = iris$Species, pch = c(16, 17, 18)[as.numeric(iris$Species)] ) would also help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.