Adding color to R plot: a function

A friend of mine told me that she was spending her day colouring R plot because she never understood how to put color in them. This triggered a nerdy reaction in me that I had to put in a basic function. This was actually a funny exercise for two reasons: forced me to think at the place of someone else (called empathy which I may be lacking), made me discover how to pass argument to a function that will be interpreted as column names without specifying them between quotes.

Enough blabla here is the code:

#plotting data with the standard plot function and coloring points according to a factor variable
#get all elements as vectors
arguments x y factor if(class(factor)!="factor"){
lvl<-length(levels(factor)) if(lvl>7){
cat("More than 7 levels in the provided factor, not enough colors available\n")

And here is an example on how to use it, just copy paste the above code, put it into some text file save as plot.col.R in a folder and then apply the below code:

#### A script for easier plotting of colours ####
#set the working directory where you put the plot.col.R file
#load the function
#load the color library
#some fake data
#the plot.col function need four arguments: the name of the x column (x), the name of the y column (y), the name of the column with the factor (factor), the name of the dataset (data), the title of the legend (title.legend)
#all settings normally used in plot can be used
plot.col(x=Temperature,y=Attack,factor=Treatment,data=fake,title.legend="Treatment",pch=3,cex=1.4,xlab="Temperature (°)",ylab="Attack rates (%)",main="Relationship between the temperature\n and the attack rates")
#if the title of the legend is not provided the plot will appear but without a legend and with a warning
#not more than seven levels are allowed in the factor variables
#the name of the levels should not be longer than 16 characters

Here is the results:

example2Which is nice.

For the people interested in a bit of theory here is how the col argument work: by providing a vector of colours as long as the number of points, R give to each points the colors specified at the position in the color vector  of the point.

Happy plotting!

2 thoughts on “Adding color to R plot: a function

  1. 🙂 know this is an old post, but any reason not to simply do the following?

    plot(fake[,1:2], col=fake[,3])
    legend(“topright”,legend=unique(fake[,3]), col=fake[,3], pch=1)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s