Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing layout of boxplot and adding labels to it

I want to (got suggestion to do so) create boxplot with different look and added labels to it. The expected (not complete) output will look like the following (with every box has quatile labeles) and sample size.

enter image description here

 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose", names = supp )

  # some  unsuccessful trials 
 # to add names 
 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose", names = supp*dose)
 # to remove the plot outline 
 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose", bty="n")
like image 346
shNIL Avatar asked Dec 20 '12 17:12

shNIL


People also ask

How do you add labels to a box plot?

Adding Labels We can add labels using the xlab,ylab parameters in the boxplot() function. By using the main parameter, we can add heading to the plot. Notch parameter is used to make the plot more understandable.

How do you change the y axis values in a boxplot in R?

To change the axis scales on a plot in base R, we can use the xlim() and ylim() functions.

How do you report boxplot results?

Box Plots and How to Read ThemThe box ranges from Q1 (the first quartile) to Q3 (the third quartile) of the distribution and the range represents the IQR (interquartile range). The median is indicated by a line across the box. The “whiskers” on box plots extend from Q1 and Q3 to the most extreme data points.


1 Answers

This should get you started. The key is to know that if you save the results of boxplot into an object while setting plot = FALSE, you get all the information for where each of the objects goes. Then you can use this information to add text via text.

d <- boxplot(len~supp*dose, data=ToothGrowth,plot = FALSE)

 boxplot(len~supp*dose, data=ToothGrowth, notch=TRUE,
  col=(c("gold","darkgreen")),
   main="Tooth Growth", xlab="Suppliment and Dose",axes = FALSE )

for (i in 1:ncol(d$stats)){
    text(i,d$stats[,i],labels = d$stats[,i],cex = 0.75,adj = c(0.5,0))
    text(i,d$stats[5,i]+1,labels = paste0("n=",d$n[i]),cex = 0.75)
    text(i-0.25,d$stats[3,i],labels = d$names[i],adj = 1,cex = 0.75)
}

I would, however, point out that whoever suggested that you:

  • remove the axes
  • annotate each boxplot with the quantile values and sample sizes

should not be giving advice to anyone about making graphs. Ever. They have successfully made your boxplot much, much worse.

enter image description here

like image 115
joran Avatar answered Sep 23 '22 02:09

joran