user1250704
user1250704

Reputation: 115

Adding labels to x axis in R graphs

I am trying to add labels corresponding to a column (one variable) onto my x axis of my graph. How can I add the labels from this variable column ?

Say I have the following table. It has three columns: Time,Treatment, and Conductivity. I have made a boxplot graph (which I cannot upload cause I don't have enough points yet and Im a new user). The plot shows three boxes per treatment though each box's label has both timepoint and treatment i.e. for EV treatment: "17.EV, 19.EV, 21.EV" for each of three boxes. How can I include the Treatment name by itself?

Time    Treatment   Conductivity
17  EV  47.1
17  EV  41.5
17  EV  53.1
17  EV  57.5
19  EV  53.2
19  EV  68.8
19  EV  69.4
19  EV  28.6
21  EV  56
21  EV  72.9
21  EV  73
21  EV  30
17  Z1a 86
17  Z1a 108
17  Z1a 81.1
17  Z1a 60.5
19  Z1a 74
19  Z1a 90
19  Z1a 109
19  Z1a 98
21  Z1a 84
21  Z1a 96.3
21  Z1a 114
21  Z1a 109.8
17  Z1b 53.3
17  Z1b 60.6
17  Z1b 56.2
17  Z1b 40.5
19  Z1b 61.2
19  Z1b 69.1
19  Z1b 64.1
19  Z1b 49.6
21  Z1b 63.5
21  Z1b 75.8
21  Z1b 73.3

Upvotes: 1

Views: 5412

Answers (2)

mathematical.coffee
mathematical.coffee

Reputation: 56905

See ?boxplot, the names argument:

names: group labels which will be printed under each boxplot. Can be a character vector or an expression (see plotmath).

So you could do:

boxplot(Conductivity ~ Treatment + Time, 
        names=rep(levels(dat$Treatment),each=3), 
        data=dat)

The levels(dat$Treatment) returns c("EV","Z1a","Z1b"), and rep(xxx,each=3) returns c("EV","EV","EV","Z1a","Z1a","Z1a","Z1b","Z1b","Z1b) (since there are 9 boxplots, 9 names are required).

If you only wanted one name per 3 boxplots - I'm not sure how to do that with base graphics, you will probably have to use ggplot2 or lattice graphics.

As an aside -- if you're producing 9 boxplots (ie one for each (Treatment,Time) pair), do you really want to remove the 'Time' information from the boxplots? It'll then be impossible to tell at which Time a particular boxplot is for?

Upvotes: 0

Justin
Justin

Reputation: 43255

I assume you're using code something like:

boxplot(data=dat, Conductivity ~ Treatment + Time)

I'm a big fan of the ggplot2 package. And would solve the problem with it.

The solution is quick and easy!

library(ggplot2)

dat <- read.table('clipboard', header=T)
ggplot(dat, aes(colour=factor(Time), x=Treatment, y = Conductivity))+geom_boxplot()

Upvotes: 1

Related Questions