Memra

Bar for categories, histogram for a numeric, line for time, scatter for two numerics

◈ 8 cards

The variable types choose the chart: one categorical → bar/column of counts (barplot(table(x))); one numeric → histogram (hist(x)); time → line; two numerics → scatter (plot(x, y)); parts of one whole → pie, reluctantly.

The type decides the chart

Module 2 sorted every column into a type — categorical or numeric, and for numerics discrete or continuous. That sorting now does a second job: it picks the chart. Take the orders columns in turn.

prov — one categorical. The only thing to show is how many (or how much) per category: a column chart (vertical bars) or a bar chart (horizontal). The R call counts first and draws second:

> table(orders$prov)

AB BC ON QC 
 2  3  4  3 
> barplot(table(orders$prov), main = "Orders by province")

barplot() wants the heights — the counts — not the raw column. Hand it the character column and it fails with a message that says nothing about tables:

> barplot(orders$prov)
Error in -0.01 * height : non-numeric argument to binary operator

Column or bar? Same chart, rotated. Go horizontal when the category labels are long (product names, not two-letter provinces) or the bars are many — Beginning Excel's guide is that past about twenty bars a column chart's labels collide. Sort the bars by value unless the categories have their own order (months, size bands).

amount — one numeric. The question is how are the values spread: a histogram, which bins the numeric into equal-width classes and draws a column per bin with no gaps. hist(orders$amount, main = "Order amount") bins the twelve values itself; Module 4 built the same 0–300, 300–600 … bins with FREQUENCY. A histogram is not a bar chart of the twelve amounts in order-id order — that draws twelve columns and shows nothing about the distribution.

Monthly totals — a numeric over time. Jan 1610, Feb 2560, Mar 1970: a line chart, one point per period joined left to right. Time is the horizontal axis; the line says how it moved. In base R, plot(1:3, c(1610, 2560, 1970), type = "l") — the type = "l" draws lines rather than points.

adspend and sales — two numerics. Does one move with the other? A scatter plot, one dot per store: plot(storessales). Module 13 fits the line through it.

Channel share — parts of one whole. Online 3060 of 6140 (49.8 %), Store 3080 (50.2 %): a pie is legal here — two slices of one total — but a bar chart shows the same two numbers and can be read to the dollar. Lesson 8.4 is about the pies that are not legal.

Type two calls, then choose six charts

Type the bar-chart and histogram calls. The questions give a variable, or a pair, and ask for the chart — the standing confusion is bar versus histogram.

CRISP-DM: the chart per variable type is data understanding → explore data; the histogram is also the verify data quality glance that shows an outlier.

variable type(s)questionchartR callone categoricalhow many percategory?column / barbarplot(table(prov))one numerichow are the valuesspread?histogramhist(amount)numeric over timehow did it move?lineplot(month, total,type = "l")two numericsdo they movetogether?scatterplot(adspend,sales)parts of one wholewhat share is eachpart?pie (≤ 6 slices)pie(table(channel))Vectors are shown bare; on the data frame they read orders$prov, orders$amount. barplot() wants counts,so table() goes inside it; hist() bins the raw numeric itself.
Chart type from variable type. Read the row for the columns in hand; the R call in the last column is what Module 11 will type against the real data frame.
NORMAL ~/memra/learn/afm-112/chart-type-by-variable-type utf-8 LF