user3396383
user3396383

Reputation: 11

How to assign different colours to different categories of data in R

I've only just started with R and we've been asked to do some simple statistical analysis on the iris data set. One of the questions was to plot a scatter of Sepal length against Sepal width and see if there is a trend. This is what I've got:

plot(iris[,1], iris[,2]) # Scatter Plot of the 1st and 2nd Columns
lines(lowess(iris[,1], iris[,2]), col = "red") # No apparent Correlation

The question then goes on to say, use a different colour for each category, ie the one for the Sepal length and one for the Sepal width. I was thinking about something like this, but it's obviously wrong:

c == col("blue", "green")
plot(iris[,1], iris[,2], col = c) # Scatter Plot of the 1st and 2nd Columns
lines(lowess(iris[,1], iris[,2]), col = "red") # No apparent Correlation

This is probably very simple but nothing I've tried seems to work. Any help would be greatly appreciated.

Upvotes: 0

Views: 592

Answers (1)

Konrad
Konrad

Reputation: 18585

Or you can simply do that

data(iris)
require(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species)

The code will generate the chart below ggplot example

Upvotes: 1

Related Questions