Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add name to boxplot in R

Tags:

plot

r

boxplot

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.

like image 236
rnso Avatar asked Dec 05 '22 05:12

rnso


2 Answers

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.

like image 148
thelatemail Avatar answered Dec 20 '22 07:12

thelatemail


Maybe you can use 'xlab':

boxplot(ddf[,1], xlab="apple")
like image 27
Andrew.T Avatar answered Dec 20 '22 08:12

Andrew.T