colouring plot by factor using custom colours in r

colors, plot, r

Solution

You can manually set the R palette used by your `plot` call like so:

palette(c("blue","pink","green"))

Which you can reset like so:

palette("default")

Try it out, creating two plots, one with default colours, one with the new colours specified:

# default plotting
palette("default")
plot(iris$Sepal.Length, iris$Sepal.Width, col=iris$Species, pch=19)

# after specifying custom palette
palette(c("blue","pink","green"))
plot(iris$Sepal.Length, iris$Sepal.Width, col=iris$Species, pch=19)

Problem

I am trying to make a scatter plot coloured by factor. I am using the following code: ``` data<-iris plot(data$Sepal.Length, data$Sepal.Width, col=data$Species) ``` Is there anyway I can colour by the species factor but specify my own custom colours? Having a look around on Google it seems it is possible to do using ggplot2 but I have never used it and was hoping I could do this using the basic R functions. Any help would be greatly appreciated!

Original source

Related problems