Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Box Plot


Share

Box Plots

A box plot shows how a set of numbers is spread out, in one small picture. It shows the middle value, where most of the values are, and values that are far away from the rest.

Use the boxplot() function to draw a box plot. Here, we use the same test scores as in the Histogram chapter:

Example

scores <- c(55, 62, 68, 70, 71, 75, 78, 80, 82, 85, 88, 91, 95)

boxplot(scores)

Result:

Box plot of test scores
Try it Yourself »

How to Read a Box Plot

  • The thick line inside the box is the median: the middle value, when all the values are sorted. Half of the scores are below it, and half are above it.
  • The box shows where the middle half of the values are.
  • The lines above and below the box (called whiskers) go out to the lowest and the highest values that are not outliers.
  • A value that is very far from the rest is drawn as a dot. It is called an outlier.

In this example, one student scored only 20. That score is far below the others, so it is drawn as a dot:

Example

scores <- c(20, 62, 68, 70, 71, 75, 78, 80, 82, 85, 88, 91, 95)

boxplot(scores)

Result:

Box plot with one outlier drawn as a dot
Try it Yourself »


Title, Label and Color

Use main to add a title, ylab to add a label, and col to change the color of the box:

Example

scores <- c(55, 62, 68, 70, 71, 75, 78, 80, 82, 85, 88, 91, 95)

boxplot(scores, main = "Test Scores", ylab = "Score", col = "lightblue")

Result:

Light blue box plot with a title and a label
Try it Yourself »

Compare Groups

Box plots are very useful when you want to compare groups. Give boxplot() more than one vector, and use names to label each box:

Example

class_a <- c(55, 62, 68, 70, 71, 75, 78, 80, 82, 85, 88, 91, 95)
class_b <- c(48, 55, 60, 63, 65, 70, 72, 74, 79, 85)

boxplot(class_a, class_b, names = c("Class A", "Class B"))

Result:

Two box plots side by side, for Class A and Class B
Try it Yourself »

Now it is easy to see that Class A has a higher median than Class B.


Horizontal Box Plot

Use horizontal = TRUE to draw the box plot from left to right:

Example

scores <- c(55, 62, 68, 70, 71, 75, 78, 80, 82, 85, 88, 91, 95)

boxplot(scores, horizontal = TRUE)

Result:

Horizontal box plot
Try it Yourself »


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.