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 ggplot2


Share

ggplot2

In the earlier Graphics chapters, you used R's built-in functions, like plot() and barplot().

ggplot2 is a package for drawing graphs, and one of the most popular R packages. It is already installed in our editor, so you only need to load it with library(ggplot2).

In ggplot2, you build a graph in layers, with a + sign between each layer:

  • ggplot() sets the data, and aes() tells which columns go on the x-axis and the y-axis.
  • A geom_ function tells how to draw the data: as points, bars, lines, and so on.
  • More layers can add titles, colors and themes.

Scatter Plot

This scatter plot shows the weight (wt) and the miles per gallon (mpg) of the cars in the mtcars data set:

Example

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()

Result:

ggplot2 scatter plot of car weight and miles per gallon
Try it Yourself »

ggplot() gets the data frame and the columns, and geom_point() draws one point for each car.

Note: Put the + sign at the end of a line, not at the start of the next line. Otherwise R thinks the graph ends on the first line.


Title, Labels and Themes

Use labs() to add a title and labels, and a theme like theme_minimal() to change the look of the whole graph:

Example

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Weight and Miles per Gallon", x = "Weight", y = "Miles per gallon") +
  theme_minimal()

Result:

ggplot2 scatter plot with a title, axis labels and a minimal theme
Try it Yourself »


Colors by Group

This is where ggplot2 really shines. Add color to aes(), and ggplot2 gives each group its own color, and adds a legend for you:

Example

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  labs(color = "Cylinders")

Result:

ggplot2 scatter plot where each number of cylinders has its own color
Try it Yourself »

factor(cyl) tells ggplot2 that the number of cylinders is a category (see R Factors), and not a number on a scale.


Bar Chart

Use geom_col() to draw a bar chart. The fill argument sets the color of the bars:

Example

library(ggplot2)

fruits <- data.frame(
  Fruit = c("Apple", "Banana", "Cherry"),
  Amount = c(10, 25, 15)
)

ggplot(fruits, aes(x = Fruit, y = Amount)) +
  geom_col(fill = "lightblue")

Result:

ggplot2 bar chart of three fruits
Try it Yourself »

Histogram

Use geom_histogram() to draw a histogram. ggplot2 works with data frames, so we put the test scores in a data frame first. The binwidth argument sets how wide each bin is:

Example

library(ggplot2)

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

ggplot(scores, aes(x = Score)) +
  geom_histogram(binwidth = 10, fill = "lightblue", color = "black")

Result:

ggplot2 histogram of test scores
Try it Yourself »

Base R or ggplot2?

Both are good choices. The built-in functions are quick for simple graphs. ggplot2 needs a little more code, but makes it easy to color groups, add legends, and make graphs that look good.



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.