Boxplot in R statistical package

Problem

Plotting a boxplot for many groups in desired order

Solution

1. Create a file (example.txt) in the format:

group     value
a           1
a           2
b           5
c           10
a           2
b           6
c           11

2. Start R and do:

file=read.table(“example.txt”, header=T);

mat=data.frame(file)

boxplot(mat$value~mat$group, as.data.frame(mat))

Look for additional parameters for boxplot in R documentation.

This works, but you may want to order your boxes in a different way. If you look online, many people will say that simply doing

group <- factor(group, levels=c(“c”, “a”, “b”))

will work.

It didn’t work for me.

What works is:

mat$group <- factor(as.character(mat$group), levels=c(“c”, “a”, “b”))

boxplot(mat$val~mat$group, as.data.frame(mat))

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s