The Basics: Visualization Basics

Session 2: Visualization Basics

2.1 Understanding ggplot2

There are many ways to Rome, and there are many ways to create a graphic using different packages and function in R. 😃

We will be focusing on ggplot2, a package part of the tidyverse, which is great in creating beautiful graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.

Go through the RStudio Primer on Data Visualization Basics and complete the assignments below.

 

Tip 1: Again, cheatsheets will make your (R) life easy! Click here to download the cheatsheet for Data Visualization with ggplot2.

Tip 2: Check out the R graph gallery for an example of graphics and code.

Assignment

In our Github page, there is a data called taxation, which is data from Basel on income and inequality.

  1. Create a data set called taxation using the file on our Github page. This time, download the data on your computer and read it in R locally. Click this link to access the folder.

  2. Create a plot using this data set. Make a plot with year and a variable of interest (e.g. Median Income), and show the different quarters. Include a title, subtitle, legend, and rename the axis labs.

  3. Save the plot as a png.

Below is an example of a plot showing the median income of the quarters by year. Note. If you want to control the order of the discrete variables, you need to know some wrangling. Although you will learn about these functions in later sessions, you can already try to sort the variable “quarter” with arrange(year, yourvariableofinterest) and turn it into a factor with mutate(quarter = as_factor(quarter)).