- Lab
- Data

Visualize Data with ggplot2 in R
n this lab, you will utilize your R skills to transform tabular data into insightful bivariate and multivariate visualizations. You will create bar charts, line charts, histograms, and scatter plots using a medical patient dataset. Additionally, you will enhance these charts by applying R techniques to improve clarity and readability.

Path Info
Table of Contents
-
Challenge
Introduction
Visualize Data with ggplot2 in R
Hello learners!
In this lab, you will utilize your R skills to transform tabular data into insightful visualization. This lab will take you through guided steps on to write
ggplot2
functions and customize them to your own needs.You will use a medical patients dataset 🩺🥼💉to achieve your lab objectives.
Key Concepts
-
Basic Plot Creation
- Bar Chart
- Line Chart
- Histogram
-
Data Manipulation
- Age Calculation
-
Chart Customization
- Custom Colors
- Labels and Titles
- Scale Adjustment
-
Multi-variable Plots
- Facet Grids
- Scatter Plot
- Trend Line
- Multiple Variables
-
Chart Formatting and Styling
- Custom Themes
- Coordinate Flipping
- Annotations
Project Structure
The FILETREE holds the following folders and files:
data
: It contains thedata.csv
file which has the following columns:patient_id
patient_name
gender
date_of_birth
visit_date
symptoms
diagnosis
medication
dosage
src
: It contains the following four files where you will write your code:step1.R
step2.R
step3.R
step4.R
Rplots.pdf
: Whenever you generate a plot, it will be saved inside this PDF file. You can download it to view the resultant plot.
All set, let’s move! 🏃🏻♀️➡️
-
-
Challenge
Creating basic charts
Your first lab objective includes a series of tasks, ranging from loading the dataset to creating bar charts, line charts, and histograms.
For this step, you will work in the
src/step1.R
file, which contains the required libraries and a pre-filleddata_with_month
function to assist you.Begin with the first task to kick off your journey! > ℹ️ NOTE
You can save R files with either the
.r
or.R
extension, as R recognizes both as valid script formats. However, if you attempt to reference axyz.r
file within anabc.r
file usingxyz.R
, it will fail because R treats the extensions as different. -
Challenge
Customize charts
In this step, you will customize the bar chart and line chart that you have created in the
step1.R
file.You will work in the
src/step2.R
file, which already includes theggplot2
library and thesrc/step1.R
file. This file retrieves the data frame using theload_data
function and stores the result in thedata
variable. -
Challenge
Create multivariate charts
Up to this point, you have created charts that rely on a single column. Now, you will explore how to build charts using multiple variables.
For this step, you will work in the
src/step3.R
file, which includes the required library and imports. This file also contains two pre-defined functions:filtered_data
– Returns a subset of the data frame containing only four diagnoses: Viral Infection, Chronic Insomnia, Hay Fever, and Allergic Rhinitis.add_age_month
– Returns a data frame with the corresponding patient age and visit month.
-
Challenge
Format and style charts
In the final step, you will learn to format and style charts. You will perform the tasks inside the
src/step4.R
file which has the necessary library and imports. In this lab, you explored how to create both basic and multivariate charts, from simple plots to those with multiple subplots. Additionally, you learned how to enhance their aesthetics for better readability and tailor them to your specific needs.Keep up the learning! ✌
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.