2Bar Plots
In this section, we introduce the fundamentals of bar plots with ggplot and Seaborn and show their main features. Bar plots, like scatterplots, will be used extensively in following chapters, with more details and variants, so as to appreciate their flexibility.
Dataset
Air Quality Report year 2021 (transl. Report qualità aria 2021), Open Data Municipality of Milan, Italy (https://dati.comune.milano.it/dataset/ds413-rilevazione-qualita-aria-2021).
Copyright: Creative Commons CC BY-4.0.
2.1 R: ggplot
A bar plot (or bar chart) is the reference type of graphic when categorical variables are handled: each category has a value associated, and a bar is drawn to represent it. Values could depend on another variable, for example, a statistic, or could represent the number of observations that fall in each category. Let us consider a first example using data about the air quality of the city of Milan, Italy, which is a heavily polluted city. It is a time series where, for each day of the period, quantities of some pollutants are measured. The variable pollutant is categorical, and we want to graphically represent the variations of pollutant levels during the time period. Column names have been translated into English.
df=read_csv2("datasets/Milan_municipality/qaria_datoariagiornostazione_2021.csv")df=rename(df, c(station_id=stazione_id, date=data,pollutant=inquinante, value=valore))head(df)# A tibble: 6 × 4station_id date pollutant value<dbl> <date> <chr> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access