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")
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.
To change the axis scales on a plot in base R, we can use the xlim() and ylim() functions.
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.
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:
should not be giving advice to anyone about making graphs. Ever. They have successfully made your boxplot much, much worse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With