Adding color to R plot: a function

February 24, 2014

(This article was first published on biologyforfun » R, and kindly contributed to R-bloggers)

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!

Filed under: R and Stat Tagged: plot, R

To leave a comment for the author, please follow the link and comment on their blog: biologyforfun » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)