Creative Commons License
Drekendrop | Blog of Tutorial by Mei Pakpahan is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at drekendrop.blogspot.com.
Permissions beyond the scope of this license may be available at http://softdadesign.co.nr.

Share This

Wednesday, August 7, 2013

Create Boxplot with legend from text file and coloring them in R

So i have been playing around with R today (this is my very first time using it), a statistical tool and i wanted to draw a boxplot analysis for my research. I am not going to explain what boxplot is here (you may find some resources explaining about it through the internet but here's one in Wikipedia), but more to how to create it in R. So if you have R already installed on your machine let's start it.

First of all, i used R Commander (Rcmdr) to import my txt file to R dataset.
1st step ----

If you have Rcmdr package already, you may skip to '2nd step ----'. But if you don't, following are the instructions.
> Go to Packages > Install package(s) > Choose your CRAN mirror. In my case i chose the closest mirror to where i am now. After you choose it, there will be a Packages window pops up, and find Rcmdr. It may ask you any other packages that you need to install, just do whatever the program says :P

2nd step ----

Go to Packages > Load package > select Rcmdr
When the R Commander window comes up,
Go to Data > Import data > from text, clipboard, or URL ... 
Enter your dataset name : (in my case, i typed 'hydro')
And i used tab as the field separator in my txt file. Then locate your txt file, and open it. It should be loaded immediately.

3rd step ----

Get back to your R console and we are ready to draw our boxplot. At first, we need to define how many colors are we going to have in our boxplot to divide the boxplot type. For example, in my case, i divided my data that each two boxplots are divided into three categories: high, medium, low;So there will be 6 boxplots (2 high, 2 medium, and 2 low).

We need to define the colors first. Type this into R console:
colors = c(rep("dark grey",2),rep("white",2),rep("light grey",2))

Now, draw the boxplot:
boxplot(YOUR_DATASET_NAME,col=colors,xlab="LABEL_FOR_X_AXIS",ylab="LABEL_FOR_Y_AXIS",main="BOXPLOT_TITLE")

in my case,
boxplot(hydro,col=colors,xlab="Protein",ylab="Number of clusters",main="Hydrophobic Clusters")

So now we have our boxplot image.

4th step -----

Last, create legend for our boxplot.
legend(x=X_COORDINATE,y=Y_COORDINATE,legend=c("FIRST_LABEL","SECOND_LABEL","THIRD LABEL"),fill=c("dark grey","white","light grey"))

Here's mine,
legend(x=4.6,y=22,legend=c("High Overlap","Medium Overlap","Low Overlap"),fill=c("dark grey","white","light grey"))

And we're done! Look at my Boxplot :)

Good luck with yours!

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms