CLiREN-LMS
Data Visualization and Dashboards

Creating Basic Charts with ggplot2

Creating Basic Charts with ggplot2

30-45 minutes Applied Step 5 of 11
Accordion

Creating Basic Charts with ggplot2

5 / 11
Accordion

Creating Basic Charts with ggplot2

Part 1
The `ggplot2` package creates plots by combining data, mappings, and layers. A simple bar chart of enrollment by site can be created as follows: The `ggplot()` function defines the dataset and aesthetic mapping. The `aes(x = site)` mapping places site on the x-axis. The `geom_bar()` layer counts records in each site category. The `labs()` function adds readable labels. This plot is useful only if each row represents one participant. If the dataset has repeated visits, the chart may overcount participants. For a participant-level enrollment summary from a dataset with repeated rows, create the summary first:
Part 2
The `distinct()` function ensures that each participant is counted once per site. The `geom_col()` layer uses precomputed counts. `coord_flip()` makes long site names easier to read. The `reorder()` function orders sites by enrollment count. A histogram can show the distribution of age: The choice of bin width affects interpretation. A very small bin width may create a noisy chart, while a very large bin width may hide meaningful patterns. The data manager should experiment but avoid manipulating bins to exaggerate a point.
Part 3
Box plots are useful for comparing numeric distributions across groups: This plot can reveal differences in age distribution by site, but it should be interpreted with sample size. A site with five participants may have a box plot that looks unstable. It may be useful to add counts in a companion table. The best way to learn visualization is to create simple plots, inspect them, and ask whether they answer the intended question.