问题
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.

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")
回答1:
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.

来源:https://stackoverflow.com/questions/13977561/changing-layout-of-boxplot-and-adding-labels-to-it