Charts for Discrete Data
Univariate Charts
Suppose we have a frequency table, perhaps created by
tab1a <- table(do3$nrooms)
tab1ap <- prop.table(tab1a)*100
We now may create a chart either from the frequencies (first line) or the percentages (second line), using the respective table. The table with percentages in my example looks like this (the first row are the data values, the second the percentages):
1 2 3 4 5 6 7 8
3 17 27 25 9 11 5 3
A plot representing the percentages by spikes is produced by
plot(tab1ap)
Alternatively, you may use barplot(tab1ap)
The following longer version of this command (with other data in mind!) demonstrates a few options:
barplot(tab1c, ylim=c(0,300), xlab="Status",
names.arg=c("Single", "Married", "Divorced", "Widow/er"), col="lightblue")
Here, xlim
ensures that the scale of the y axis ranges from 0 to 300, xlab
will add a label to the x axis, names.arg
provides labels for the bars (e.g., if the table has no labels, or if you are not pleased by those that there are), and col
controls the color of the bar (which by default is grey). Obviously, this does not exhaust the possibilities.
Bivariate Charts
Stacked Bar Charts
With barplot
, you can create stacked bar charts. This website explains how.
Mosaic Plots
A better alternative are mosaic plots, aka as Marimekko plots. These are the default if plot
is applied to two categorical variables, as in
plot(do3$gender, do3$education)
The result looks like this (obviously in need of elaboration, which perhaps I'll do later):
The data are a sample of full-time employees in Germany; hence the small proportion of women, who often are employed part-time.
© W. Ludwig-Mayerhofer, R Guide | Last update: 22 Jun 2025