Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 39 ยท Visualization

Choosing the Right Chart in R: Distributions to Time Series

Pick the right chart for your question: distributions, comparisons, relationships, change over time and parts of a whole, with ggplot2 recipes and honest-design rules.

Knowing how to draw a bar chart is not the same as knowing when a bar chart is the right answer. The best chart depends on the question you want the reader to answer, and on the types of your variables. This lesson gives you a decision guide, a ggplot2 recipe for each case, and the rules that keep a chart honest.

R does not run on this site, so the recipes below are read-only. They use the built-in mtcars and iris data, with the charts described in words. See ggplot2 Basics for the syntax and Customizing ggplot2 for polishing.

Start with the question

flowchart TD
  Q["What do you want to show?"] --> D["How values are distributed"]
  Q --> C["Compare groups or categories"]
  Q --> R["How two numbers are related"]
  Q --> T["Change over time"]
  Q --> P["Parts of a whole"]
  D --> D1["Histogram, density, box plot"]
  C --> C1["Bar chart, dot plot, box plot"]
  R --> R1["Scatter plot, smoother, 2D bins"]
  T --> T1["Line chart"]
  P --> P1["Stacked bar, or a plain table"]

Match the question to the chart family, then check the variable types.

Distribution of one numeric variable

Questions: What is typical? How spread out? Any outliers or two humps?

R
ggplot(mtcars, aes(mpg)) + geom_histogram(binwidth = 2)    # counts per bin
ggplot(mtcars, aes(mpg)) + geom_density()                  # smooth shape
ggplot(mtcars, aes(y = mpg)) + geom_boxplot()              # five-number summary

Histograms show shape (skew, two peaks) but depend on the bin width: try two or three before concluding anything. Box plots summarize but can hide a bimodal shape; add geom_jitter() or use geom_violin() to keep it visible. For comparing several groups, draw one box or violin per group.

Comparing categories

Questions: Which group is bigger? Do the groups differ?

R
ggplot(mtcars, aes(factor(cyl))) + geom_bar()                         # counts
ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()                # distribution per group
ggplot(summary_df, aes(reorder(group, mean_mpg), mean_mpg)) +
  geom_col() + coord_flip()                                           # sorted means

Bars encode length from zero, so their baseline must be zero. Sort bars by value unless the categories have a natural order (months, age bands). When you show a mean with bars, also show the spread, for example with geom_errorbar() or, better, the raw points. A dot plot (geom_point() on a sorted category axis) is a clean alternative when you have many categories.

Relationship between two numbers

Questions: Do they move together? Is the relationship linear? Are there clusters?

R
ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth(method = "lm")
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point()
ggplot(big_data, aes(x, y)) + geom_bin2d()          # 2D bins for hundreds of thousands of points

A scatter plot is almost always the right start. With thousands of overlapping points, lower alpha, shrink the points, or switch to 2D bins/hexagons (geom_hex()). Remember that a visible trend is not proof of cause: see the lessons on correlation and regression.

Change over time

Questions: Is it rising or falling? Seasonal? Any breaks?

R
ggplot(economics, aes(date, unemploy)) + geom_line()
ggplot(long_df, aes(date, value, color = series)) + geom_line()

Lines connect points that belong together in time order, so use them for time and ordered steps, never for unrelated categories. Keep the time axis evenly spaced and avoid more than about five lines on one chart; use facets for more. For counts over time, columns can work, but lines scale better to long periods.

Parts of a whole

Questions: What share does each part contribute?

R
ggplot(mtcars, aes(x = "all cars", fill = factor(cyl))) +
  geom_bar(position = "fill")        # one stacked bar scaled to 100%

Pie charts ask readers to compare angles, which people do badly. Use a sorted bar chart of the shares, or a single stacked bar for two or three parts. If exact numbers matter, show a table.

Honest design: five rules

  • Start bars at zero. A truncated bar axis exaggerates differences. Lines and dot plots may use a non-zero range when the range is stated.
  • Do not use 3D or decorative effects. They distort sizes and add nothing.
  • Do not rely on color alone. Add shapes, labels or direct annotations, and use a colorblind-safe palette.
  • Label the axes with units and put the source on the chart.
  • Do not hide the data. If a chart summarizes (a mean, a box), consider showing the points too.

The first rule matters more than it looks. The block below is a Python stand-in that measures how much a truncated axis exaggerates a small difference (Edward Tufte's "lie factor": the size of the effect in the picture divided by the size of the effect in the data).

Python Python comparison: how a truncated axis exaggerates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With a zero baseline the lie factor is 1: the picture tells the truth. Starting the axis at 95 makes a 4 percent difference look like a 133 percent one.

Quick reference

Variables First choice Alternatives
1 numeric histogram density, box plot, dot plot
1 categorical bar chart sorted dot plot
numeric by category box or violin plot jittered points, means with intervals
2 numeric scatter plot smoother, 2D bins
numeric over time line chart area for cumulative totals
2 categorical stacked or grouped bars heat map of counts (geom_tile())
3 numeric scatter plus color or size facets, bubble chart
geography map (geom_sf()) choropleth, points on a map

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which chart is the best first choice for the relationship between two numeric variables?

    A scatter plot shows each pair of values and lets you see trends, clusters and outliers.
  2. 2. Why must the baseline of a bar chart be zero?

    Readers compare bar lengths. If the bars are cut off, small differences look enormous.
  3. 3. What should you do when a scatter plot has 200,000 overlapping points?

    alpha, smaller points or geom_bin2d()/geom_hex() show the density that overplotting hides.
  4. 4. Which geom is the right tool for a change over time?

    Lines connect ordered points and emphasize trends.

Summary

  • Choose the chart by the question and the variable types, not by habit.
  • Distributions: histogram, density, box or violin. Groups: bars from zero, or box plots. Relationships: scatter plot. Time: lines. Parts of a whole: sorted bars rather than pies.
  • Keep charts honest: zero baselines for bars, no 3D, no color-only encoding, labeled units.
  • Show the raw data whenever you can.

Next: Descriptive Statistics in R