This is the R script repository of the "Data Visualization 3: Practical Data Visualization with R" course in the 2023/2024 Winter term, part of the MSc in Business Analytics at CEU. For the previous editions, see 2019/2020 Spring, 2020/2021 Winter, 2021/2022 Winter, and 2022/2023 Winter.
3 x 100 mins on Jan 24 and 31:
- 13:30 - 15:10 session 1
- 15:10 - 15:40 break
- 15:40 - 17:20 session 2
- 17:20 - 17:40 break
- 17:40 - 19:20 session 3
In-person at the Vienna campus (QS B-421).
Please find in the syllabus
folder of this repository.
Please bring your own laptop* and make sure to install the below items before attending the first class:
- Join the Slack channel dedicated to the class (
#ba-dv3-2023
) - Install
R
from https://cran.r-project.org - Install
RStudio Desktop
(Open Source License) from https://posit.co/download/rstudio-desktop/ - Register an account at https://github.com
- Enter the following commands in the R console (bottom left panel of RStudio) and make sure you see a plot in the bottom right panel and no errors in the R console:
install.packages(c('ggplot2', 'gganimate', 'transformr', 'gifski'))
library(ggplot2)
library(gganimate)
ggplot(diamonds, aes(cut)) + geom_bar() +
transition_states(color, state_length = 0.1)
Optional steps I highly suggest to do as well before attending the class if you plan to use git
:
-
Bookmark, watch or star this repository so that you can easily find it later
-
Install
git
from https://git-scm.com/ -
Verify that in RStudio, you can see the path of the
git
executable binary in the Tools/Global Options menu's "Git/Svn" tab -- if not, then you might have to restart RStudio (if you installed git after starting RStudio) or installed git by not adding that to the PATH on Windows. Either way, browse the "git executable" manually (in somebin
folder look for theegit
executable file). -
Create an RSA key (optionally with a passphrase for increased security -- that you have to enter every time you push and pull to and from GitHub). Copy the public key and add that to you SSH keys on your GitHub profile.
-
Create a new project choosing "version control", then "git" and paste the SSH version of the repo URL copied from GitHub in the pop-up -- now RStudio should be able to download the repo. If it asks you to accept GitHub's fingerprint, say "Yes".
-
If RStudio/git is complaining that you have to set your identity, click on the "Git" tab in the top-right panel, then click on the Gear icon and then "Shell" -- here you can set your username and e-mail address in the command line, so that RStudio/git integration can work. Use the following commands:
$ git config --global user.name "Your Name" $ git config --global user.email "Your e-mail address"
Close this window, commit, push changes, all set.
Find more resources in Jenny Bryan's "Happy Git and GitHub for the useR" tutorial if in doubt or contact me.
(*) If you may not be able to use your own laptop, there's a shared RStudio Server set up in AWS for you - including all the required R packages already installed for you. Look up the class Slack channel for how to access, or find below the steps how the service was configured:
💪 RStudio Server installation steps
# most recent R builds
wget -q -O- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
echo "deb [arch=amd64] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/" | sudo tee -a /etc/apt/sources.list.d/cran_r.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 67C2D66C4B1D4339 51716619E084DAB9
sudo apt update && sudo apt upgrade
sudo apt install r-base
# apt builds of all CRAN packages
wget -q -O- https://eddelbuettel.github.io/r2u/assets/dirk_eddelbuettel_key.asc | sudo tee -a /etc/apt/trusted.gpg.d/cranapt_key.asc
echo "deb [arch=amd64] https://r2u.stat.illinois.edu/ubuntu jammy main" | sudo tee -a /etc/apt/sources.list.d/cranapt.list
sudo apt update
# install some R packages
sudo apt install -y r-base gdebi-core r-cran-ggplot2 r-cran-gganimate
sudo apt install -y cargo libudunits2-dev libssl-dev libgdal-dev desktop-file-utils
sudo apt install -y r-cran-data.table r-cran-rcpp r-cran-dplyr r-cran-ggally r-cran-pander r-cran-readxl
sudo apt install -y r-cran-ggrepel r-cran-hexbin r-cran-animation r-cran-dendextend r-cran-nbclust
sudo apt install -y r-cran-ggmap r-cran-maps r-cran-devtools r-cran-ggraph r-cran-ggthemes
sudo apt install -y r-cran-leaflet r-cran-mapproj
wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2023.12.0-369-amd64.deb
sudo gdebi rstudio-server-*.deb
# never do this in prod
echo "www-port=80" | sudo tee -a /etc/rstudio/rserver.conf
sudo rstudio-server restart
💪 Creating users
secret <- 'something super secret'
users <- c('list', 'of', 'users')
library(logger)
library(glue)
for (user in users) {
## remove invalid character
user <- sub('@.*', '', user)
user <- sub('-', '_', user)
user <- sub('.', '_', user, fixed = TRUE)
user <- tolower(user)
log_info('Creating {user}')
system(glue("sudo adduser --disabled-password --quiet --gecos '' {user}"))
log_info('Setting password for {user}')
system(glue("echo '{user}:{secret}' | sudo chpasswd")) # note the single quotes + placement of sudo
log_info('Adding {user} to sudo group')
system(glue('sudo adduser {user} sudo'))
}
- Warm-up exercise and security reminder: 1.R
- Intro / recap on R and ggplot2 from previous courses by introducing MDS: 1.R
- Geocoding: 1.R
- Shapefiles: 1.R
- Scaling / standardizing variables: 1.R
- Simpson's paradox: 1.R
- Anscombe's quartett 1.R
- Intro to datasaurus 1.R
- Review homework: 2.R and homework-solutions.rmd
- Recap on
data.table
summaries: 2.R - Warm-up exercises on
ggplot2
calls and EDA 2.R - Extract points from a plot 2.R
- Hierarchical clustering 2.R
- Animations 2.R
- Themes 2.R
- Interactive plots 2.R
To be updated from week to week.
- Load the
nycflights13
package and check what datasets are bundled. - Visualize the distribution of arrival delays per origin airport!
- Visualize the distribution of arrival delays per destination airport! Note that the x axis labels need to be rotated, and spend some time cleaning up the axis and plot titles! Might need to tweak
fig.height
andfig.width
params of the code chunk in Rmd! - Compute and visualize the average arrival delay per destination! Make sure to handle
NA
s and order the barplot by the average delay. - Redo the above plot by showing the actual name of the destination airport instead of it's FAA id!
- Color the bars by the timezone of the airport! Make sure to render the legend on the top, using a single line.
- Geocode the destination airports, then visualize those on a worldmap with the point sizes relative to the number of flights to there from NY!
- Compute the average departure and arrival delays, also the average air time and distance per destination, then pass the scaled dataframe through MDS to visualize the similarities/dissimilarities of the destination airports!
If in doubt about what is expected and how the results and outputs should look like, see this example solution.
Submission: prepare an R Markdown or Quartro document that includes the exercise as a regular paragraph then the solution in an R code chunk (printing both the code and its output) and knit to HTML or PDF and upload to Moodle before Jan 31 noon (CET).
Use any publicly accessible dataset (preferably from the TidyTuesday projects), but if you don't feel creative, feel free to pick the palmerpenguins
dataset and demonstrate what you have learned in this class by generating different data visualizations that makes sense and are insightful, plus provide comments on those in plain English. This can totally be a continuation of your Intro to R submission.
Required items to include in your work for grade "B":
- at least 5 plots (with at least 3 different
ggplot2
geoms) not presented yet in any of your previous CEU projects - a meaningful animation using
gganimate
(instead of presenting random stuff moving around, make sure that rendering an animation for your use-case makes sense, e.g. showing how things changed over time) - either (1) register an account at stadiamap to fetch map tiles or (2) use shapefile(s) to present some geospatial data (e.g. points or polygons rendered on a background map)
For grade "A":
- make sure to fine-tune your plots and make those pretty by always setting proper (axis) titles, scales, custom color palettes etc.
- define a custom theme (e.g. background color, grid, font family and color) and use that on all (or at least on most) plots
- at least one interactive plot
Submission: prepare an R Markdown or Quartro document that includes the exercise as a regular paragraph then the solution in an R code chunk (printing both the code and its output) and knit to HTML or PDF and upload to Moodle before Feb 18 midnight (CET).
File a GitHub ticket.