rnso
rnso

Reputation: 24545

Add name to boxplot in R

This question is related to: R: how to label the x-axis of a boxplot

When more than one column is plotted, names appear. But when only one column is plotted, name does not appear, even when names=.. argument is used:

ddf = structure(list(apple = c(1, 2, 3, 4, 5), banana = c(5, 4, 3, 
 2, 1), watermelon = c(4, 5, 6, 7, 8)), .Names = c("apple", "banana", 
 "watermelon"), row.names = c(NA, -5L), class = "data.frame")

 ddf
  apple banana watermelon
1     1      5          4
2     2      4          5
3     3      3          6
4     4      2          7
5     5      1          8


boxplot(ddf[,1:2])
boxplot(ddf[,1])

enter image description here

enter image description here

Following also do not work:

boxplot(ddf[,1], names='apple')
boxplot(ddf[,1], names=c('apple'))

How can I add name to the boxplot when only one column is used? Thanks for your help.

Upvotes: 6

Views: 25284

Answers (5)

thelatemail
thelatemail

Reputation: 93813

There is a show.names= argument to bxp, which boxplot calls. You can thus do:

boxplot(ddf[1], show.names=TRUE)

Make sure this is ddf[1] not ddf[,1] though, so that the name is retained.

Upvotes: 9

Alex
Alex

Reputation: 13

I also used the solution with show.names for Boxplot{car}. In my case I wanted to sum up some columns in one boxplot and label the outliers at the same time, hence I used Boxplot.

Boxplot(df, show.names = T, names = "test samples", labels = rownames(df), id.method = c("y"), id.n=9)

For boxplot you don't need to support a list of names for show.names if you are satisfied with the names of your dataframe. For Boxplot you have to supply a name for the plot.

Upvotes: 1

koekenbakker
koekenbakker

Reputation: 3604

The boxplot is added at x=1 by default, so you can add at tick and axis label to x=1 as would happen when you plot multiple columns.

axis(side = 1, at = 1, labels = 'apple')

Upvotes: 1

Andrew.T
Andrew.T

Reputation: 420

Maybe you can use 'xlab':

boxplot(ddf[,1], xlab="apple")

Upvotes: 5

Marc in the box
Marc in the box

Reputation: 11995

One way is to use mtext:

boxplot(ddf[,1])
mtext("apple", side=1, line=1)

Upvotes: 2

Related Questions