R Lesson 5: ggplot2 Charts

R Lesson 5: ggplot2 Charts featured illustration

πŸ“Ž This article includes 2 downloadable practice files ↓

⏱ 1 min readUpdated 12 October 2026

πŸ“˜ R Beginner Course Β· Lesson 5 of 6

by_region <- sales |> group_by(region) |> summarise(sales = sum(amount))
ggplot(by_region, aes(x = reorder(region, sales), y = sales / 1e5)) +
  geom_col(fill = "#c2410c") + coord_flip() +
  labs(title = "Sales by region", x = NULL, y = "Rs lakh") + theme_minimal()
ggsave("sales_by_region.png", width = 6, height = 4)

Every ggplot has data, aesthetics (which column goes on x, y, colour) and a geometry (geom_col bars, geom_line lines, geom_point dots). Layers are added with +.

Advertisement
πŸ’‘ reorder(region, sales) sorts bars by value, the single biggest readability improvement for bar charts.

More: ggplot2 for Excel users.

Code for this course follows standard, current syntax; we test JavaScript and PowerShell scripts before publishing, and R/Java/Rust examples are kept to core language features. Tell us if anything fails on your setup.

Advertisement

Practice

Run the script and open the saved PNG.

πŸ“Ž Practice files for this article

⬇ Download all 2 files (ZIP Β· 811 B)

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong