Reputation: 3521
How to put values on boxplot and control its width?
X<-c(1,2,,3,4,4,5,5,6,6,6,6,6,7)
I need to write values for min, max, 1st quartile, median and last quartile. How can I put it there?
Upvotes: 8
Views: 64506
Reputation: 143
boxplot(X, axes = FALSE, staplewex = 1)
text(y = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, x = 1.25)
How to do this but in multiple Boxplot
Example data:
a <- data.frame(
Rank = c(1,2,3,1,2,3,1,2,3,3,2,1,3,2,1,2,1,3,3,3,1,1,2,2,1,3 ),
Time = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3),
Value = c(5,10,15,20,30,50,70,80,100,5,7,9,11,15,17,19,17,19,100,200,300,400,500,700,1000,200))
boxplot(a$Value[a$Rank==1] ~ a$Time[a$Rank==1])
Upvotes: 0
Reputation: 23
I had some problems trying to understand the boxplot quartiles labels and compare to summary function values, so i´d like to share with you.
Sometimes there will be differences in quartiles labels in boxplot using fivenum or stats comparing to r summary function values. This occurs only on even datasets.
e.g.(using text(x = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, y = 1.25)):
product<-c(3,12,20,25,30,35,70,70,80,150)
summary(product)
Min. 1st Qu. Median Mean 3rd Qu. Max.
3.00 21.25 32.50 49.50 70.00 150.00
boxplot(product,horizontal=TRUE,col="grey",staplewex=1,axes=FALSE)
text(x = boxplot.stats(product)$stats, labels = boxplot.stats(product)$stats, y = 1.25)
As you can see in the picture, the values do not match.
In this case you can use quantile function:
text(x=quantile(produto),labels=quantile(produto),y=1.25)
Now you should get the same values as listed on summary function. Otherwise just ignore summary function and use fiveNum values instead. The differences occurs due to lack of universal agreement among statisticians.
A google search will show you the differences on quantiles calculation.
Upvotes: 1
Reputation: 13570
The answer provided by mnel is perfect, assuming —as in the example— that there are not outliers. In that case, the lower and upper whiskers are then equal to the minimum and maximum. But, if there are outliers and we don't specify range = 0
in the boxplot function, we don't get the right values for the labels. Instead, we could use:
text(x=boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, y = 1.25)
Let's see an example:
Horizontally
X <- c(1,2,3,3,4,4,5,5,6,6,6,6,10,15)
boxplot(X, horizontal = TRUE, axes = FALSE, staplewex = 1)
text(x = fivenum(X), labels = fivenum(X), y = 1.25)
text(x = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, y = 1.25)
Vertically
Switching the arguments for x and y inside text
.
boxplot(X, axes = FALSE, staplewex = 1)
text(y = boxplot.stats(X)$stats, labels = boxplot.stats(X)$stats, x = 1.25)
Upvotes: 17
Reputation: 115390
You can use horizontal = TRUE
get a horizontal boxplot and axes = FALSE
to remove the axes. staplewex = 1
sets the staple width the same as the box width
Then you can use fivenum
to return the statistics used to create the boxplot and use these as text labels, fiddling with the y
value until you have what you want
boxplot(X, horizontal = TRUE, axes = FALSE, staplewex = 1)
text(x=fivenum(X), labels =fivenum(X), y=1.25)
Note that i've inserted a 3
for the value missing in your example data X
Upvotes: 20